mongodb - Group array after unwind and match -


i have twice nested schema:

mongoose.model('team', mongoose.schema( {  players : [{      trikots : [{         isnew : boolean,         color : string     }]  }] }) 

for now, query looks this:

team.aggregate()         .match({'_id' : new objectid(teamid)})         .unwind('players')         .unwind('players.trikots')         .match({'players.trikots.isnew' : 'red', 'players.trikots.isnew' : true})         .exec(sendback); 

but have 1 team object, contains players array. how can achieve that?

use group on _id $push operator return players 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 want other field included in final doucment add _id field during group operation.

.group({'_id':{'_id':'$_id','some_other_field':'$some_other_field'},'players': {'$push': '$players'}}) 

Comments