How to Update Document in MongoDB

MongoDB provides the update() method to update the documents of a collection. To update only the specific documents, you need to add a condition to the update statement so that only selected documents are updated. 

In this article, we will understand how to update a document in MongoDB using the update() method, save () method, updateOne() method and updateMany() method with examples for each. We will also look at the differences between each of these methods. 

Syntax: db.collection.update(query, update, options) 

Syntax: db.collection.update(query, update, options) 

For updating multiple documents at the same time in MongoDB, we need to use the multi option or else by default only one document is updated at a time.

The below example shows how to update many documents. In this example, we are going to first find the document which has the Emp_ID id as “1” and then change the Emp_ID from 1 to 21 and Employee_Name to “Nandan Kumar”. 

The db.collection.save() method is used to update an existing document or insert a new document 

Syntax: db.collection.save() 

The save() method returns a WriteResult object which contains the status of the insert or update operation. 

During the insert, the shell will create the _id field with a unique ObjectId value, as verified by the inserted document:

In the below example, save() method performs an insert since the document passed to the method does not contain the _id field so it creates a new document . 

Note –  If the document doesn’t contain an _id field, then the save() method calls the insert() method. 

The save() method performs an update with upsert: true since the document contains an _id field:  

Note –  

If the document already contains an _id field, then the save() method is equivalent to an update with the upsert option set to true and the query predicate on the _id field. 

Updating Single and Multiple Values in MongoDB documents by using methods  -updateOne, updateManywith examples : 

This method updates a single document within a collection matching the filter or condition. 

Syntax 

The syntax of updateOne() method is − 

Example

The updateMany() method updates all the documents within a collection based on the filter or condition . 

Syntax : 

The syntax of updateMany() method is − 

Example

Using the find command, you can retrieve the contents of the documents:

If the update operation doesn’t match any documents in the collection, it can automatically insert a new document into the collection which matches the update query by setting the upsert option as true.

WriteResult ({“nMatched”: 0,  “nUpserted: 1, “nModified”:1 })   

You can also see the upsert getting reflected in the Write Result of the above operation. 

upsert operation in MongoDB is used to save documents into collection . 

If the document matches query criteria, then it will perform an update operation or else it will insert a new document into the collection. 

The difference is that update() by default, modifies only one document based on the specified filter. However, the user can modify all the documents by adding the modifier {multi: true} . 

This command works as both updateOne and updateMany command. 

Here, it will update only first document which matches the condition.

Here, by adding the parameter – multi: true it works as updateMany() and updates all the documents matching the condition .

db.collection.updateOne() –> method to update only one document in a collection.

This update commands use the joinDate =2020 as a filter (match the query) in the collection “Employees”. $set operator (called as update operator) updates the value of the bonusEligiblity to False.

You can also update multiple parameters but they need to be separated by a comma (,). E.g.

db.collection.updateMany() –> method to update multiple document in a collection matching the specified condition

Here, ALL the documents having joinYear =2020 get updated to bonus Eligiblity= “False” 

If the update operation doesn’t match any documents in the collection, it can automatically insert a new document into the collection which matches the update query by setting the upsert option as true.

WriteResult({“nMatched”: 0,  “nUpserted: 1, “nModified”:1 })  

You can also see the upsert getting reflected in the WriteResult of the above operation.

upsert operation in MongoDB is used to save documents into collection.

If the document matches query criteria then it will perform an update operation or else it will insert a new document into the collection.
upsert  also partially updates an object in MongoDB so that the new object will overlay or merge with the existing one.

In brief, upsert is also used to update a document with the contents of another document, but only updates the fields that are absent and completely ignore the fields that are already set.

To summarize, MongoDB has methods: update() and save() which are used to update documents into a collection. The update() method updates the values in the existing document while the save() method is used to insert a new document or update an existing document if it already contains an _id field The parameters in the document.update() method is a condition for which document needs to be updated, and the next is the update operation which needs to be performed. 

db.collection.update (query, update, options) 

In this article, we have gone over the update() method, save () method, updateOne() method and updateMany() method with examples for each. We have also explored the upsert function. 

Hope this has been useful. ThMongoDB course will get you familiar with the popular NoSQL database and arm you with the essential skills to start using Mongo to power your software application. Happy coding! 

  • Use the update method  
  • Specify the condition to be used to update the document. In the below example, we need to update the document which has the Employee id as 100. 
  • Use the $set operator to update the values  
  • Choose the Field Name that needs to be updated and enter the new values accordingly –  
  • Use the update method  
  • Specify the condition which should be used for updating the document. In the below example, we need to update the document which has the Employee id as 1. 
  • Use the $set operator to update the values  
  • Choose the Field Name(s) that needs to be updatedand enter the new values accordingly – 
  • Research & References of How to Update Document in MongoDB|A&C Accounting And Tax Services
    Source