Pio
Pio

Reputation: 4062

Mongoose findandUpdate multiple field of document

I am trying to update multiple fields of a document:

 Game.findByIdAndUpdate(gameId, {
    $addToSet: {
        players: player.id
    },
    $addToSet: {
        playersInfo: player
    }, function(err, model){...}
}

But this query only executes my last $addToSet

Upvotes: 1

Views: 4651

Answers (1)

heinob
heinob

Reputation: 19464

In JavaScript an object cannot have the same key multiple times. So the second overwrites the first.

Try this:

Game.findByIdAndUpdate(gameId, {
    $addToSet: {
        players: player.id,
        playersInfo: player
    }, function(err, model){...}
})

Upvotes: 2

Related Questions