Last Updated: 14 August, 2023
In MongoDB, we can update or modify one or more existing documents in a collection using the following methods:
| Method Name | Description |
|---|---|
| db.collection.updateOne(<filter>, <update>, <options>) | Used to update a single document within the collection based on the filter. |
| db.collection.updateMany(<filter>, <update>, <options>) | Used to update multiple documents within the collection based on the filter. |
| db.collection.replaceOne(<filter>, <update>, <options>) | Used to replace a single document within the collection based on the filter. |
We can specify the condition for update operations, which means we update specific documents in a collection based on the requirement. These conditions work the same as read operations in MongoDB.
In MongoDB, All write operations are atomic at the level of a single document.
To update a document, MongoDB provides update operators such as $set to modify field values.
For example, suppose we have a collection named student which has the following documents as given below:
[
{sid: 1, name: 'Ramesh', class: 'Nursery', roll: 15, gender: 'M'},
{sid: 2, name: 'Gyatri', class: 'LKG', roll: 18, gender: 'F'},
{sid: 3, name: 'John', class: 'Nursery', roll: 26, gender: 'M'},
{sid: 4, name: 'Priya', class: 'UKG', roll: 10, gender: 'F'},
{sid: 5, name: 'Suraj', class: 'LKG', roll: 12, gender: 'M'}
]
The updateOne() method is used to update a single document in a collection. See the examples below.
Example 1: Update single document into a collection
db.student.updateOne({sid: 2}, {$set: {roll: 55}});
Here, updating single document, if sid is 2 then update the roll field with new value.
Output
/* 1 /
{
"acknowledged" : true,
"matchedCount" : 1,
"modifiedCount" : 1
}
Example 2: Update two fields in a single document
db.student.updateOne({sid: 2}, {$set: {roll: 55, class: 'STD-1'}});
Here, updating two fields in a single document, if sid is 2 then update the roll and class fields with new values.
Output
/* 1 /
{
"acknowledged" : true,
"matchedCount" : 1,
"modifiedCount" : 1
}
MongoDB also allows us to update multiple documents in a collection using the updateMany() method. See the examples below.
Example 3: Update multiple documents in a collection
db.student.updateMany({class: 'Nursery'}, {$set: {class: 'Play Group'}});
Here, we are updating multiple documents whose class field value is Nursery to new value Play Group.
Output
/ 1 */
{
"acknowledged" : true,
"matchedCount" : 2,
"modifiedCount" : 2
}
Example 4: 'AND' operator with update documents
db.student.updateMany(
{class: "Nursery", gender: "M"},
{$set: {class: "LKG"}})
Here, we are updating documents with AND condition that means only those documents will be updated whose class is 'Nursery' and gender is 'M'.
Output
/ 1 */
{
"acknowledged" : true,
"matchedCount" : 2,
"modifiedCount" : 2
}
Example 5: 'OR' operator with update documents
db.student.updateMany(
{$or: [{class: 'LKG'}, {gender: 'F'}]},
{$set: {class: "UKG"}})
Here, we are updating documents with OR condition that means those all documents will be updated whose class is 'LKG' or gender is 'F'.
Output
/ 1 */
{
"acknowledged" : true,
"matchedCount" : 3,
"modifiedCount" : 3
}
The replaceOne() method replaces the first matching document in the collection that matches the filter, using the replacement document.
Example 6: Replace a document into a collection
db.student.replaceOne(
{class: 'Nursery'},
{sid: 110, name: 'Dhiraj', roll: 25, gender: 'M'});
Here, this will replace first matching document of given criteria.
Output
/ 1 */
{
"acknowledged" : true,
"matchedCount" : 1,
"modifiedCount" : 1
}
Reference: https://www.mongodb.com/docs/mongodb-shell/crud/update/
That's all, guys. I hope this MongoDB article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com