How can I change a specific value inside a document?

I am kinda new to MongoDB.

image

I have a document structured like can be seen in the above image and I want to update only one of the keys using the db.update_one() function.

For example, I would like to change the value of key 180731688844787712 to a different number. I would like to do this without updating the whole document (Since this is just an example and the documents that will be used in the project will have many more key-value pairs, updating the whole document is probably going to be inefficient.)

I tried taking a look at the help documentations and Google but couldn’t find an answer.

Thanks a lot in advance.

1 Like

Hi,

You can update fields with $set operator. You can do it like this:

db.collection.update({
  "_id": "1"
},
{
  "$set": {
    "value.key_1": 10,
    "value.key_4": 40,
    "value.key_5": 50
  }
})

Working example

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.