We may not have the course you’re looking for. If you enquire or give us a call on 01344203999 and speak to our training experts, we may still be able to help with your training requirements.
We ensure quality, budget-alignment, and timely delivery by our expert instructors.
As an integral part of Data Management, updating data plays a pivotal role in maintaining the accuracy and relevance of your database. You can effortlessly manage and update your documents when working with MongoDB, a leading NoSQL database. In essence, performing a MongoDB Update Document implies modifying the existing data in your database.
So, how to update a document in MongoDB? This updating can range from altering individual fields, adding new ones, or removing existing ones. The capability to update in MongoDB is made possible through a variety of methods such as updateOne(), updateMany(), and replaceOne(). Further, read this blog to learn more about the commands used in MongoDB to update the document.
Table of Contents
1) Understanding MongoDB Update operations
2) How to update documents in MongoDB
3) MongoDB Update Document example
a) Example using updateOne()
b) Example using updateMany()
c) Example using replaceOne()
4) Conclusion
Understanding MongoDB Update operations
MongoDB is one of the most popular NoSQL databases. This offers a host of operations that can be performed on documents. Among these, the update operations are pivotal as they help keep the database dynamic and relevant.
Updating operations in MongoDB involves modifying existing documents in a collection. The modifications can take various forms - from changing the values of existing fields, adding new fields to a document, or completely replacing an existing document.
There are three primary methods for updating documents in MongoDB: updateOne(), updateMany(), and replaceOne().
1) The updateOne() method is used to update a singular document that matches the provided filter.
2) The updateMany() method is employed when you need to update all the documents that match the specified filter.
3) The replaceOne() method replaces an entire document that matches a specified filter.
Transform your coding skills today with our comprehensive App & Web Development Training.
How to update documents in MongoDB?
Let us now understand how updating a document in MongoDB can be performed:
1) Using the updateOne() Method: MongoDB's updateMany() method updates all documents that match the provided filter. It's incredibly useful when a broad update is required across numerous documents. The syntax of this method is:
db.collection.updateOne(filter, update, options)
Here, 'filter' is a document that specifies the match condition. 'update' is a document that specifies the modifications, and 'options' is an optional document that specifies additional options.
2) Using the updateMany() Method: The updateMany() method in MongoDB updates all documents that match the provided filter. It's incredibly useful when a broad update is required across numerous documents. The syntax of this method is:
db.collection.updateMany(filter, update, options)
Similar to updateOne(), 'filter' specifies the match condition, 'update' the modifications, and 'options' provides additional options.
3) Using the replaceOne() Method: The replaceOne() method replaces the entire document that matches the filter, except for the _id field. The syntax for this method is:
db.collection.replaceOne(filter, replacement, options)
In this case, 'filter' is the match condition, 'replacement' is the new document that replaces the old one (excluding _id), and 'options' includes additional specifications.
MongoDB Update Document example
To understand MongoDB's document update functionalities better, let's go through some examples for each update method:
a. Example using updateOne()
Suppose we have a collection named students and we want to update the grade of the student named 'John'. Here's how we would use the updateOne() method:
db.students.updateOne({ name: 'John' }, { $set: { grade: 'A' }})
This command looks for the first document where the name is 'John' and updates the grade field to 'A'.
Let’s have a look at another example:
Suppose we want to update the age of a student named 'Mary' in the students' collection:
db.students.updateOne({ name: 'Mary' }, { $set: { age: 22 }})
This command updates the age field to 22 for the first document where the name is 'Mary'.
b. Example using updateMany()
Let's say we want to update the grade for all students named 'John'. In this case, we would use the updateMany() method:
db.students.updateMany({ name: 'John' }, { $set: { grade: 'A' }})
This command updates the grade field to 'A' for all documents where the name is 'John'.
Let us discuss another example:
Let's say we want to update the grade for all students in a specific class in the student's collection:
db.students.updateMany({ class: '10A' }, { $set: { grade: 'B' }})
This command updates the grade field to 'B' for all documents where the class is '10A'.
c. Example using replaceOne()
To replace an entire document, we use the replaceOne() method. If we want to replace the entire document for a student named 'John' with a new document, we can do the following:
db.students.replaceOne({ name: 'John' }, { name: 'John', grade: 'A', age: 20 })
In this example, the entire document where the name is 'John' is replaced with the new document provided. The _id field of the old document remains unchanged.
Let us see another example:
If we want to replace the entire document for a product with the id of '123' in the products collection, we can do:
db.products.replaceOne({ _id: '123' }, { _id: '123', name: 'Laptop', price: 1200, status: 'In Stock' })
{
"acknowledged" : true,
"matchedCount" : 1,
"modifiedCount" : 1,
"upsertedId" : null
}
Here's what each part means:
"acknowledged": true indicates that the operation was successful.
"matchedCount": 1 means that one document matched the filter condition { _id: '123' }.
"modifiedCount" : 1 signifies that one document was modified, i.e., the old document was replaced by the new one.
"upsertedId" : null shows that no new document was inserted as part of an upsert operation (in this case, we did a replace operation, not an upsert).
In the event that there was no document matching { _id: '123' }, "matchedCount" and "modifiedCount" would be 0, indicating that no document was found and hence no replacement occurred.
Conclusion
MongoDB Update Document operations are essential tools for managing data in your database. Understanding how to utilise updateOne(), updateMany(), and replaceOne() methods efficiently can greatly enhance your ability to manipulate data. This also helps to maintain the flexibility and integrity of your MongoDB database.
Frequently Asked Questions
Upcoming Programming & DevOps Resources Batches & Dates
Date
Fri 23rd May 2025
Fri 25th Jul 2025
Fri 26th Sep 2025
Fri 28th Nov 2025