j-da
j-da

Reputation: 188

Increment nested value

I create players the following way.

Players.insert({
  name: name,
  score: 0,
  items: [{'name': 0}, {'name2': 0}...]
});

How do I increment the score in a specific player and specific item name (upserting if necessary)?

Sorry for the terrible wording :p

Upvotes: 0

Views: 608

Answers (2)

j-da
j-da

Reputation: 188

Well, the answer is - as in life - to simplify the problem by breaking it up.

And to avoid arrays in mongoDB - after all, objects can have as many keys as you like. So, my structure became:

{
  "_id": <id>,
  "name": <name>,
  "score": <score>,
  "items": {}
}

And to increment the a dynamic key in items:

// create your update skeleton first
var ud = { $inc: {} };

// fill it in
ud.$inc['item.' + key] = value;

// call it
db.Players.update(player, ud, true);

Works a charm :)

Upvotes: 3

elyase
elyase

Reputation: 40963

Lets say you have:

{
    "_id" : ObjectId("5465332e6c3e2eeb66ef3683"),
    "name" : "Alex",
    "score" : 0,
    "items" : [
        {
            "food" : 0
        }
    ]
}

To update you can do:

db.Players.update({name: "Alex", "items.food": {$exists : true}}, 
                  {$inc: {score: 1, "items.$.food": 5}})

Result:

{
    "_id" : ObjectId("5465332e6c3e2eeb66ef3683"),
    "name" : "Alex",
    "score" : 1,
    "items" : [
        {
            "food" : 5
        }
    ]
}

I am not sure you can upsert if the document doesn't exist because of the positional operator needed to update the array.

Upvotes: 0

Related Questions