Updated February 28, 2023
Introduction to MongoDB Update
MongoDB Update method is used to update the document from the collection, and the update method will update the value of the existing document. We have used a $set operator at the time of updating the document. We can update a single document by using an update or updateOne method. If we want to update multiple documents in a single update command, we must update many methods. Using the update method, we can update a single as well as multiple documents in one statement. The update method is essential and useful in MongoDB to update the document.
Syntax
Below is the syntax of the update method:
collection_name.update (document)
OR
collection_name.update (< query>, <update>)
{
Upsert: <Boolean>
Multi: <Boolean>
Writeconcern: <document>
Collation: <document>
ArrayFilters: [<filterdocument1>, …]
} )
OR
collection_name.updateOne (Document) – Update single document
OR
collection_name.updateMany (Multiple Documents) – Update multiple document
OR
collection_name.findOneAndUpdate (Selection_critera_of_document, Updated_data_of_document)
Parameter description of MongoDB Update
- Collection name: Collection name is defined as update documents from the collection by using the update method. We can update single and multiple documents at one time using updateOne and update many methods.
- Document: Document is defined as data that we have to update collection by using an update method. We can update single or multiple documents at one time.
- Write concern: This is an optional parameter of the update method.
- Query: Query defines as a selection criterion of a document for the update operations. Using the query, we have to define select criteria of update.
- Update: Update method is used to update the document from the collection; the update method will update the value of the existing document. The update method is essential and useful.
- Upsert: It is an optional parameter of the update method. It will create a new document when our updated query is not matched with the existing document.
- Multi: It will update multiple documents from a single query when our query matches the criteria. It is an optional parameter of the update method.
- Collation: Collation is used to allow users to specify language-specific rules for the string comparison. It is an optional parameter of the update method.
- Array Filters: It is an optional parameter of the update method. It will filter an array that specifies which documents elements modify an update operation in the array field.
- Update one: Update one method is used to update a single document. If we want to update a single document, then we have using the update method.
- Update Many: Update many methods is used to update multiple documents. If we want to update multiple documents in a single statement, we have used many methods.
- Find one and update: This method is used to find and update the document.
How Update command works in MongoDB
Below is the working of the update command in MongoDB.
- The update method is essential and useful in MongoDB to update the document.
- Using the update method, we can update single and multiple documents in one statement in MongoDB.
- We can update a single document by using an update or updateOne method. If we want to update multiple documents in a single update command, we have to update many MongoDB methods.
- MongoDB update method is used to update the document from the collection; the update method in MongoDB will update the existing document’s value.
- We have used a $set operator at the time of updating the document.
- While using the Upsert method in MongoDB, it will insert the document when the document is not present in the collection. The collection is also automatically created at the time of document updating using the Upsert method in MongoDB.
- The below example shows the Upsert method will automatically insert the document when this is not present in the collection.
use db_update
db.books_update.update ({item: "1"},{book_name: "story", stock: 5, tags: ["database"]}, {upsert: true})
db.books_update.find()
db.books_update.update ({item: "2"},{book_name: "story_book", stock: 10, tags: ["database"]}, {upsert: true})
db.books_update.find()
Explanation: In the first example, we have inserted a document using an update and upsert method in MongoDB. At the time of insertion, we have created a books_update collection.
Examples to Implement MongoDB Update
Below is the example of an update method in MongoDB. We have taken an example of an emp_count table to describe examples of MongoDB’s update method as follows. Below is the data description of the emp_count table are as follows.
Code:
use emp_count
db.emp_count.find ()
Output:
Example #1
Update single documents using the update method: In the below example, we have updated a single document using the update method. We have updated emp_id value from 6 to 101 by using an update method in MongoDB.
Code:
db.emp_count.update({'emp_id': 6},{$set:{'emp_id': 101}})
db.emp_count.find ()
Output:
Example #2
Update single documents using the updateOne method: In the below example, we have updated a single document using the update method. We have updated the emp_id value from 5 to 100 by using the update method.
Code:
db.emp_count.updateOne({'emp_id': 5}, {$set: {'emp_id': 100}})
db.emp_count.find ()
Output:
Example #3
Update multiple documents using the update many methods: In the below example, we have updated multiple documents using the update many methods. We have updated emp_name value from XYZ to CBA by using the update many methods in MongoDB.
Code:
db.emp_count.updateMany ({'emp_name': "XYZ"}, {$set:{'emp_name': "CBA"}})>
db.emp_count.find ()
Output:
Conclusion
MongoDB update method is used to update the document from the collection; the update method in MongoDB will update the existing document’s value. We have used a $set operator at the time of updating the document. The update method is essential and useful in MongoDB to update the document.
Recommended Articles
This is a guide to MongoDB Update. Here we discuss Introduction to MongoDB Update, Syntax, How does it work, Examples with code and output. You can also go through our other related articles to learn more –