Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

115
Vistas
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 Respuestas
Responde la pregunta

0

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

mongoplayground

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda