Använd följande aggregeringspipeline för att få önskat resultat:
var pipeline = [
{
"$match": {
"name": "Andre",
"fruits.active": true,
"cars.active": true
}
},
{ "$unwind": "$fruits" },
{ "$unwind": "$cars" },
{
"$match": {
"fruits.active": true,
"cars.active": true
}
},
{
"$group": {
"_id": {
"_id": "$_id",
"name": "$name"
},
"cars": { "$addToSet" : "$cars" },
"fruits": { "$addToSet" : "$fruits" }
}
},
{
"$project": {
"_id": 0,
"name": "$_id.name",
"cars": 1,
"fruits": 1
}
}
]
m_object.aggregate(pipeline)
.exec(function (err, result) {
if (err) {
console.log(err);
return;
}
console.log('result');
});
Eller så kan du använda aggregationspipeline byggare enligt följande:
m_object.aggregate()
.match({
"name": "Andre",
"fruits.active": true,
"cars.active": true
})
.unwind("fruits")
.unwind("cars")
.match({
"fruits.active": true,
"cars.active": true
})
.group({
"_id": {
"_id": "$_id",
"name": "$name"
},
"cars": { "$addToSet" : "$cars" },
"fruits": { "$addToSet" : "$fruits" }
})
.project({
"_id": 0,
"name": "$_id.name",
"cars": 1,
"fruits": 1
})
.exec(callback);