Mongoose is not returning Objects if sub Array documents don't satisfy the match condition in mongoose.
MongoDB Query:
db.fruits.aggregate(
{ $match: { "isActive": true, fruitId: "kipld" } },
{ $unwind: '$types' },
{ $match: { "types.isActive": true } },
{ $group: {
_id: '$_id',
name: {$first: '$name'},
price: { $first: '$price' },
types : { $push : '$types' }
}
},
{ $project: {
'_id': 1,
'name':1,
'price': 1,
'types': 1,
}
}
)
MongoDB Collection:
{
_id: "abcdefg",
fruitId: "kipld",
isActive: true,
types :[
{
name: "Kashmir Apple",
price: 90,
isActive: false
},
{
name: "American Apple",
price: 120,
isActive: false
}
]
}
Expected result will be like:
{
_id: "abcdefg",
fruitId: "kipld",
isActive: true,
types :[]
}
But when I'm running the above query I'm not getting anything.