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
}
}
},
)
db.collection.aggregate([
{
$unwind: "$incompleted"
},
{
$match: {
"incompleted.preReq": []
}
},
{
$group: {
_id: "$_id",
incompleted: {
$push: "$incompleted"
}
}
}
])