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

114
Visualizações
retrieve the object which have empty array from a particular document

I have some documents of nested object. And I want to retrieve those object group them with the nested object which have empty array using agregation.

let me explain more accurately- suppose my collection consist of documents given bellow---

document 1

{
    "_id": "17202155",
    "completed": [
      "cse331",
      "cse312"
    ],
    "incompleted": [
      {
        "name": "math",
        "preReq": []
      },
      {
        "name": "science",
        "preReq": [
          "bang",
          "eng",
          "math"
        ]
      },
      {
        "name": "astronomy",
        "preReq": [
          "bang",
          "eng",
          "science"
        ]
      }
    ]   },

document 2

{
    "_id": "17202157",
    "completed": [
      "cse331",
      "cse312"
    ],
    "incompleted": [
      {
        "name": "math",
        "preReq": []
      },
      {
        "name": "science",
        "preReq": []
      },
      {
        "name": "astronomy",
        "preReq": [
          "bang",
          "eng",
          "science"
        ]
      }
    ]
  }

i want to retrieve the documents group by the id with the object consist of empty array. That means the result will be --

{
    "_id": "17202155",
    "incompleted": [
      {
        "name": "math",
        "preReq": []
      },
      
    ]
  },
  {
    "_id": "17202157",
    "incompleted": [
      {
        "name": "math",
        "preReq": []
      },
      {
        "name": "science",
        "preReq": []
      },
    ]
  }

i have use this code but it doesnot works. Though i am new to mongodb. please help me--

#my code-

db.collection.aggregate({
  "$unwind": "$_id"
},
{
  "$group":{"_id": "$incompleted",}
},
{
  "$match": {
    "_id.preReq": {
      $size: 0
    }
  }
},
)
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

db.collection.aggregate([
  {
    $unwind: "$incompleted"
  },
  {
    $match: {
      "incompleted.preReq": []
    }
  },
  {
    $group: {
      _id: "$_id",
      incompleted: {
        $push: "$incompleted"
      }
    }
  }
])

mongoplayground

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