Increment a value in a nested object?
mongodb
Solution
The structure you want is:
{
gameName: "string",
slug: "string",
players: [ { playerName: "string", playerScore: number} ] , ...]
}
To increment player score by one if his name is Joe you would use:
db.collection.update( {"players.playerName":"Joe"}, { $inc : { "players.$.playerScore" : 1 } }
Problem
I am messing around with mongodb and node.js this weekend and I'm having some trouble getting through the mongodb docs regarding updating nested objects/documents. I have a collection in which the documents look like this.. ``` { gameName: "string", slug: "string", players: [{playerName, playerScore}, ...] } ``` I'm trying to find out how to increment the value of playerScore based on the playerName. I suppose this mostly comes down to me not understanding NoSQL method. I even have second thoughts about using an array for the players, so any input would be appreciated. Thanks!