Sign In Register

How can we help you today?

Start a new topic

update MongoDB document sub object without replacing

{
  "_id": {
    "$oid": "59a8668f900bea0528b63fdc"
  },
  "userId": "KingSlizzard",
  "credits": 15,
  "settings": {
    "music": 1,
    "sfx": 0
  }
}

I have this method for updating just specific fields in a document

function setPlayerDataField(targetUserId, updateObject) {
    playerDataCollection.update({
            "userId": targetUserId //Looks for a doc with the userId of the player
        }, { $set: updateObject }, //Uses the $set Mongo modifier to set value at a path
        false, //Create the document if it does not exist (upsert)
        true //This query will only affect a single object (multi)
    );
}

It works fine if I do a command like

setPlayerDataField("KingSlizzard",{"credits": 20});

It would result in the document like this

{
  "_id": {
    "$oid": "59a8668f900bea0528b63fdc"
  },
  "userId": "KingSlizzard",
  "credits": 20,
  "settings": {
    "music": 1,
    "sfx": 0
  }
}

The value for credits is now 20 yay! This is desired.

However, if I do this command...

setPlayerDataField("KingSlizzard",{"settings": {"music":0}});

It would result in the document like this

{
      "_id": {
        "$oid": "59a8668f900bea0528b63fdc"
      },
      "userId": "KingSlizzard",
      "credits": 20,
      "settings": {
        "music": 0
      }
    }

All I wanted to do was set only the settings/music value to 0. This result is NOT desired since we lost the sfx value.

So my question is, how do I update a value in a sub object without replacing the whole sub object itself?

1 Comment

found an answer on StackOverflow here

Login to post a comment