I have a twice nested schema:
mongoose.model(\'Team\', mongoose.Schema(
{
players : [{
trikots : [{
isNew : Boolean,
color : String
Use Group
on _id
with $push operator to return all players into an array.
Team.aggregate()
.match({'_id' : new ObjectId(teamId)})
.unwind('players')
.unwind('players.trikots')
.match({'players.trikots.color' : 'red', 'players.trikots.isNew' : true})
.group({'_id':'$_id','players': {'$push': '$players'}})
.exec(sendBack);
If you want any other field to be included in the final doucment add it to _id
field during group operation.
.group({'_id':{'_id':'$_id','some_other_field':'$some_other_field'},'players': {'$push': '$players'}})