MongoDB Update Document
You can update one or more fields in documents of a MongoDB collection based on a specific criteria, using update() method.
MongoDB Update Document – In this tutorial, we shall learn how to update MongoDB Documents based on a selection criteria using update() method.
Syntax of update() method
The syntax of update() method is
db.collection_name.update(criteria, update, options)
where
collection_name | Mame of the collection in which you update document |
criteria | [mandatory] Criteria to select documents for update |
update | [mandatory] Updates to the fields for the selected documents |
Options – All options below are optional.
Option | [Type][DefaultValue] DefaultAction | Description |
upsert | [boolean][false] Does not insert a new document when no match found | If set to true, Inserts as a new document when no match is found for the criteria. |
multi | [boolean][false] Updates only one document. | If set to true, updates all the documents that obey the criteria. |
collation | [document][default] Uses binary comparison for comparing strings. | You may specify language-specific rules. Refer Collation[https://docs.mongodb.com/manual/reference/collation/]. You can specify only only language in a Collation. |
WriteConcern | [document][default] Refer Write Concern[https://docs.mongodb.com/manual/reference/write-concern/]. | Write concern describes the level of acknowledgement requested from MongoDB. |
To update a MongoDB Document, follow the step by step guide below :
- Prepare a Criteria to select those documents for update.criteria = {field1:value1, field2:value2,..}
- Prepare Update Document.update = {field1:value1, field2:value2,..}
- [Optional] Query the Documents based on the Criteria formed to cross verify if you want to update all those selected Documents.db.collection.find(criteria)
- Prepare the options based on your use case.
- Run db.collection.update() method with criteria, update and options.db.collection.update(criteria, update, options)
Example 1 – Update single MongoDB Document
Following is an example to update only one Document.
We shall use followingpeople Collection in this Document Update Example and run the commands in Mongo Shell.
> db.people.find({}) { "_id" : ObjectId("59eeba4e5f82df4555f2bfb2"), "name" : "Midhuna", "age" : 23, "place" : "Amaravati" } { "_id" : ObjectId("59eeba4e5f82df4555f2bfb3"), "name" : "Akhil", "age" : 24, "place" : "New York" } { "_id" : ObjectId("59eeba4e5f82df4555f2bfb4"), "name" : "Honey", "age" : 27, "profession" : "Docter" } { "_id" : ObjectId("59eeba4e5f82df4555f2bfb5"), "name" : "Manju", "age" : 23, "place" : "Amaravati", "profession" : "Carpenter" } { "_id" : ObjectId("59eeba4e5f82df4555f2bfb6"), "name" : "Bharat", "age" : 24, "place" : "New York", "profession" : "Scientiest" } { "_id" : ObjectId("59eeba4e5f82df4555f2bfb7"), "name" : "Arya", "age" : 25, "profession" : "Teacher" }
Now, prepare a criteria to select a document.
> criteria={name:"Manju"} { "name" : "Manju" }
Verify the documents you select with the criteria
> db.people.find(criteria) { "_id" : ObjectId("59eeba4e5f82df4555f2bfb5"), "name" : "Manju", "age" : 23, "place" : "Amaravati", "profession" : "Carpenter" }
We shall prepare the update document as below
> update={name:"Manju",age:28,place:"Vizag"} { "name" : "Manju", "age" : 28, "place" : "Vizag" }
To update only the values you want to change, rather than mentioning all the values once again, use $set command. Next example demonstrates the usage of $set command.
Let the Options have their default value.
And execute the update command on the Collection.
> db.people.update(criteria,update) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
MongoDB has acknowledged that “nMatched” : 1 there has been one Document that matched the Criteria, and “nModified” : 1 has modified one document.
Verify the update by querying the collection with the criteria
> db.people.find(criteria) { "_id" : ObjectId("59eeba4e5f82df4555f2bfb5"), "name" : "Manju", "age" : 28, "place" : "Vizag" }
The details have been updated successfully.
Example 2 – Update multiple MongoDB Documents
In this example, we shall update multiple mongodb documents with the help of $set command in update document and option multi set to true.
We shall use followingpeople Collection in this Document Update Example and run the commands in Mongo Shell.
> db.people.find({}) { "_id" : ObjectId("59eeba4e5f82df4555f2bfb2"), "name" : "Midhuna", "age" : 23, "place" : "Amaravati" } { "_id" : ObjectId("59eeba4e5f82df4555f2bfb3"), "name" : "Akhil", "age" : 24, "place" : "New York" } { "_id" : ObjectId("59eeba4e5f82df4555f2bfb4"), "name" : "Honey", "age" : 27, "profession" : "Docter" } { "_id" : ObjectId("59eeba4e5f82df4555f2bfb5"), "name" : "Manju", "age" : 28, "place" : "Vizag", "profession" : "Driver" } { "_id" : ObjectId("59eeba4e5f82df4555f2bfb6"), "name" : "Bharat", "age" : 24, "place" : "New York", "profession" : "Programmer" } { "_id" : ObjectId("59eeba4e5f82df4555f2bfb7"), "name" : "Arya", "age" : 25, "profession" : "Teacher" }
Now, prepare a criteria to select multiple MongoDB documents.
> criteria={place:"New York"} { "place" : "New York" }
> db.people.find(criteria).count() 2
There are multiple documents selected for the criteria.
We shall prepare the update document as below, including $set command
> update={$set:{bonus:250}} { "$set" : { "bonus" : 250 } }
To update only the values you want to change, rather than mentioning all the values once again, we used $set command.
We shall set multi option to true to update multiple MongoDB documents
> options={multi:true} { "multi" : true }
And execute the update command on the Collection.
> db.people.update(criteria,update,options) WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
MongoDB has acknowledged that “nMatched” : 2 there has been two documents that matched the criteria, and “nModified” : 2 has modified all the documents that matched the criteria.
Verify the update by querying the document with the criteria
> db.people.find(criteria) { "_id" : ObjectId("59eeba4e5f82df4555f2bfb3"), "name" : "Akhil", "age" : 24, "place" : "New York", "bonus" : 250 } { "_id" : ObjectId("59eeba4e5f82df4555f2bfb6"), "name" : "Bharat", "age" : 24, "place" : "New York", "profession" : "Programmer", "bonus" : 250 }
The details have been updated successfully.
Conclusion
In this MongoDB Tutorial – MongoDB Update Document, we have learnt to update single or multiple MongoDB documents using update() method and $set command with the help of examples.