How can I change a specific value inside a document?

I am kinda new to MongoDB.


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


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

  "_id": "1"
  "$set": {
    "value.key_1": 10,
    "value.key_4": 40,
    "value.key_5": 50

Working example


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