MongoDB Update Documents – MongoDB CRUD Operations Part 2

In this chapter, we will talk about MongoDB update documents – how to update documents in the collection. There are many ways from which we can update documents in the collection:

  1. collection.updateOne()
  2. collection.updateMany()
  3. collection.update()
  4. collection.findOneAndUpdate()
  5. collection.replaceOne()
  6. collection.findOneAndReplace()
  7. collection.findAndModify()
  8. collection.save()

Let’s read about them in brief.


MongoDB Update Documents


There are many methods to update documents in MongoDB as mentioned above. Let’s go through these methods to understand it better.

1) collection.updateOne()

This method updates the FIRST document that matches the given filter. Filter is any criteria based on which we want to update document (such as age=18, color=red etc.)

Following is a sample MongoDB update command

This update commands use the age =18 as a filter in the collection “students”. $set operator updates the value of the canVote to True.

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

  • collection.updateMany()

This command is similar to db.collection.updateOne() but the only difference is that it updates ALL the documents that match the filter in the collection.

Here, ALL the documents having age=18 get updated to canVote to “True”.

  • collection.update()

This command works as both updateOne and updateMany command.

As UpdateOne() command:

Here, it will update only first document that matches the filter.

As UpdateMany() Command:

Here, by using one additional parameter – multi: true it works as updateMany() and updates all documents that match the filter.

  • collection.findOneAndUpdate()

This updates a single document based on the filter and other criteria.

The simple syntax is: db.collection.findOneAndUpdate(filterupdateoptions)

The findOneAndUpdate() method has the following form:

Here, we have some additional parameters which are as follows:

  • Projection: Optional. It denotes A subset of fields to return. If you want to return all fields in the returned document, do not include this parameter.
  • Sort: Specifies a sorting order for the documents matched by the filter.
  • returnNewDocument: It returns the updated document instead of the original document when set to true.

 

  • collection.replaceOne()

This command replaces at most a single document that matches a specified filter even though multiple documents may match the specified filter.

The replaceOne() method has the following form:

 

  • collection.findOneAndReplace()

This command modifies and replaces a single document based on the filter and sort criteria.

The findOneAndReplace() method has the following form:

  • collection.findAndModify()

This command modifies and returns a single document. But the key point to be noted here is that by default, the returned document does not include the modifications made on the update. To return the document with the modifications made on the update, use the new option.

The findAndModify() method has the following form:

  • collection.save()

This command updates an existing document or inserts a new document (If the document is not present), depending on its document parameter.

The save() method has the following form:

mongodb update documents

That’s it in this MongoDB update documents – MongoDB CRUD Operations Part 2. In the next chapter, we will read about MongoDB delete documents (CRUD Operations Part 3). If you have any queries, please comment in the comment section below. Also, do share your feedback 🙂

Mohit Arora
Follow me

Mohit Arora

Hi, I am Mohit Arora , a passionate blogger and addicted reader. Currently working as a Senior Systems Engineer in INFOSYS Ltd.
You can check my other blog - CatchUpdates.com

You can connect with me on Instagram Or Quora And Linkedin as well.
Mohit Arora
Follow me