Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
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 answers
Answer question

0

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

mongoplayground

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!