Search code examples
javascriptmongoosepopulate

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

e.g.:

"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?


Solution

  • 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']
      }]