Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

106
Visualizações
MongoDB Find in nested arrays

My goal is to;

  1. get all the documents with status=true
  2. and return only objects with active=true in the life array.

Below is how my mongoDB documents look like.

{
    "name": "justine",
    "life" : [ 
        {
            "status" : true,
            "service" : [ 
                {
                    "partner" : "pat 1",
                    "active" : true,
                }, 
                {
                    "partner" : "pat 2",
                    "active" : false
                }
        }
    ]
},
{
    "name": "okumu",
    "life" : [ 
        {
            "status" : true,
            "service" : [ 
                {
                    "partner" : "pat 1",
                    "active" : true,
                }, 
                {
                    "partner" : "pat 2",
                    "active" : true
                }
        }
    ]
}

expected output

{
    "name": "justine",
    "life" : [ 
        {
            "status" : true,
            "service" : [ 
                {
                    "partner" : "pat 1",
                    "active" : true,
                }
        }
    ]
},
{
    "name": "okumu",
    "life" : [ 
        {
            "status" : true,
            "service" : [ 
                {
                    "partner" : "pat 1",
                    "active" : true,
                }, 
                {
                    "partner" : "pat 2",
                    "active" : true
                }
        }
    ]
}

This is what i did.

await Users.find({ life: { $elemMatch: { status: true, life: { $elemMatch: { active: false } } } }});

This is working well for the first condition, in case the second condition is not met, the entire object is not returned, however, if it's met, even the active=false objects are returned.

It'll be grateful if you can help me out, am not a mongoDB expert.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think it is complex (possible not doable) with the .find() query.

You should use .aggregate() query.

  1. $match - Filter document with life.status is true.

  2. $project -

    2.1 $filter - Filter the document with status is true in life array document.

    2.1.1 $map - Merge the life document with the Result 2.1.1.1.

    2.1.1.1 $filter - The inner $filter operator to filter the document with active is true for service document in service array.

db.collection.aggregate([
  {
    $match: {
      "life.status": true
    }
  },
  {
    $project: {
      name: 1,
      life: {
        "$filter": {
          "input": {
            "$map": {
              "input": "$life",
              "in": {
                "$mergeObjects": [
                  "$$this",
                  {
                    "service": {
                      "$filter": {
                        "input": "$$this.service",
                        "as": "service",
                        "cond": {
                          $eq: [
                            "$$service.active",
                            true
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          },
          as: "life",
          "cond": {
            $eq: [
              "$$life.status",
              true
            ]
          }
        }
      }
    }
  }
])

Sample Mongo Playground

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda