MongoDB Update Document: A Practical Guide
Learn how to update MongoDB documents safely with updateOne, updateMany, and findOneAndUpdate. See clear examples for changing only the fields you need.

Contents

Updating Data in MongoDB
MongoDB data update operations let you update or modify data in the database. You can do this easily using MongoDB’s document-based structure. A MongoDB data update operation lets you perform CRUD (Create, Read, Update, Delete) operations on documents.
Data update operations most commonly use the update() and save() functions. Along with these, there are many different methods you can use to update data.
The update() Function
MongoDB data update operations can be performed using the update() function. This function can update specific fields of specific documents in the database. In the following example, the last name of a user named “John” is updated to “Doe”.
db.users.update({name: "John"}, {$set: {lastName: "Doe"}});
The save() Function
Data update operations can also be performed using the save() function. This function can completely replace a specific document. In the following example, the entire document for a user named “John” is updated.
db.users.save({_id: ObjectId("54f9d8da7f8b9a7518543e00"), name: "Jane", lastName: "Doe"});
Other Methods
Data update operations can also be performed with methods other than the update() and save() functions. These include:
- the findAndModify() function
- the updateMany() function
- the updateOne() function
- the findOneAndUpdate() function
These functions offer different ways to update documents in the database and can help you choose the one that best fits your specific needs. For example, the findAndModify() function updates the first document found by the query and returns the modified document. In the following example, the age of a user named “John” is updated.
db.users.findAndModify({query: {name: "John"}, update: {$set: {age: 30}}});
The updateMany() Function
This function updates all documents that match a given criterion at once. In the following example, the ages of all users aged 25 are updated to 30.
db.users.updateMany({age: 25}, {$set: {age: 30}});
The updateOne() Function
This function updates the first document that matches a given criterion. In the following example, the age of the first user named “John” is updated.
db.users.updateOne({name: "John"}, {$set: {age: 30}});
The findOneAndUpdate() Function
This function updates the first document that matches a given criterion and returns the modified document. In the following example, the age of the first user named “John” is updated.
db.users.findOneAndUpdate({name: "John"}, {$set: {age: 30}});
In summary, MongoDB data update operations can be performed through different functions such as update(), save(), findAndModify(), updateMany(), updateOne(), and findOneAndUpdate(). These functions are designed to help you choose the one that best fits your specific needs. Data update operations let you update or modify data in the database, and these operations are easy to carry out thanks to MongoDB’s document-based structure.
Share it if it helped


