Gebruik de volgende aggregatiepijplijn om het gewenste resultaat te krijgen:
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');
});
Of u kunt de aggregatiepijplijn gebruiken bouwer als volgt:
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);