I Have a document structured like this in mongo DB, and I want to filter to show only active subdocuments: active cars and active fruits.
{
"name":"Andre",
"fruits":[
{
"active":true,
"fruitname":"apple"
},{
"active":false,
"fruitname":"banana"
}
],
"cars":[
{
"active":false,
"carname":"ford"
},{
"active":true,
"carname":"GM"
},
]
}
this is my desired result.
{
"name":"Andre",
"fruits":[
{
"active":true,
"fruitname":"apple"
}
],
"cars":[
{
"active":true,
"carname":"GM"
},
]
}
I've tried Aggregate
but when any cars or any fruits active, it's return nothing.
m_object.aggregate([
{ $match : {
"name": "andre"
}},
{ $unwind : "$fruits" },
{ $unwind : "$cars" },
{ $match : {
'fruits.active':{$eq: true}
}},
{ $match : {
'cars.active':{$eq: true}
}},
{ $group : {
"name": "$name",
cars: { $addToSet : "$cars" }
fruits: { $addToSet : "$fruits" }
}}
], function (err, result) {
if (err) {
console.log(err);
return;
}
console.log('result');
});
Is there any way to omit the "active":false
subdocuments in those subdocuments?