Group array after unwind and match

前端 未结 1 1980
没有蜡笔的小新
没有蜡笔的小新 2020-12-19 07:23

I have a twice nested schema:

mongoose.model(\'Team\', mongoose.Schema(
{
 players : [{ 
    trikots : [{
        isNew : Boolean,
        color : String
            


        
相关标签:
1条回答
  • 2020-12-19 08:05

    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'}})
    
    0 讨论(0)
提交回复
热议问题