简体   繁体   中英

Mongoose: Populate multiple fields on the same path

I have following models: Players, Games and Tournaments In the Game there are always 2 players and a game can have multiple (0-n) reactions.

games-Model

GameSchema = new Schema({
 players: [{player, scorePoints}]
 reactions: [{player, type}]
})

tournaments-Model

TournamentSchema= new Schema({
 players: [{player, scorePoints}]
 games: [{game}]
})

in my "GET" Tournament Request, i want to answer with all user-details and need to populate 2 fields from the same "path":

tournament-service

async function getOne(id) {
    let tournament = await Tournament.findById(id)
        .populate({
            path: 'games',
              populate: {
                path:'players.player',
                select: ['username', 'avatar']
              }
        })
        .populate({
            path: 'games',
              populate: {
                path: 'reactions.player',
                select: ['username', 'avatar']
              }
        })
        .exec();

    return tournament;
}

The Problem is: since it is the same "Path", it will only populate the last field. see here: https://mongoosejs.com/docs/populate.html#populating-multiple-paths

eg:

"games:"[{
  players: [
  {"id": "1", "username": "UserOne"},
  {"id": "2", "username": "UserTwo"}
  ],
  reactions: [
   {"id": 1, "type": "angry"}, 
   {"id": 2, "type": "happy"}
 ]
}, 
{ //* next Game*// }
]

How it should be:

"games:"[{
  players: [
  {"id": "1", "username": "UserOne"},
  {"id": "2", "username": "UserTwo"}
  ],
  reactions: [
   {"id": 1, "username": "UserOne", "type": "angry"}, 
   {"id": 2, , "username": "UserTwo", "type": "happy"}
 ]
}, 
{ //* next Game*// }
]

How can i solve this?

2 Weeks later, i found a working solution: you can pass an array with all fields to populate:

 path: 'games',
 populate: [{
   path: 'players.player',
   select: ['username', 'rating', 'playedGames', 'avatar1']
  }, {
   path: 'reactions.player',
   select: ['username', 'avatar1']
  }]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM