Updating Multiple Document in MongoDB

Updating Multiple Documents in MongoDB

In this article, I am going to discuss Updating Multiple Documents in MongoDB with Examples. Please read our previous article where we discussed Updating Single Documents in MongoDB with Examples.

Updating Multiple Documents in MongoDB

To update multiple documents MongoDB provides a method named as updateMany(). This method is used to update all the documents that match the given condition in the specified collection.

This method only accepts a document that consists of an updated operation expression. It can also be used in multi-document transactions. While updating the documents the value of the _id field remains the same. Starting from MongoDB 4.2, the updateMany() method can also support an aggregation pipeline that specifies the modification. It contains the following stages:

  1. $addFileds and it’s an alias that is $set
  2. $project and it’s an alias that is $unset
  3. $replaceRoot and its allies that is $replaceWith

With the help of an aggregation pipeline, we are allowed for a more expressive update statement, for example, expressing conditional updates based on current field values, etc. When the upset:true and no document match the specified collection, then this method adds a new document according to the filter criteria and update modifications.

In the capped collection, if the update operation changes the size of the document, then the operation will fail. You can also use updateMany() method with the shared collection. This method is not compatible with db.collection.explain(). This method is also used inside multi-document transactions.

Syntax:
db.collection.updateMany(
   <filter>,
   <update>,
   {
      upsert: <boolean>,
      writeConcern: <document>,
      collation: <document>,
      arrayFilters: [ <filterdocument1>, <filterdocument2>… ],
      hint: <document|string>
   }
)

Parameter:

UpdateOne() method has the following parameters:

Parameter Type Description
Filter Document This parameter specifies the selection criteria for the update. If the value of this parameter is empty, then update all the documents in the collection.
Update Document or
pipeline
This parameter contains the modifications that are going to apply to the document. It can be:
Update document: It consists only of update operator expressions.
Aggregation Pipeline: It consists of the aggregation stages.
Upset Boolean It is optional and the default value is false.
Value is true, this method: Create a new document if no document matches the specified filter. If the match is found, then update the single document.
Value is false, this method: Does not insert a new document when no match is found.
writeConcern Document It is optional. A document expressing the written concern. Try to omit using the default write concern.
Collation Document It is Optional. It specifies the collation to use for the operation. Collation allows users to specify language-specific rules for string comparison.
arrayFilters Array It is optional. An array of filter documents is used to determine which array elements to modify for an update operation on an array field.
Hint Document It is optional. A document or a string that specifies the index to use to support query predicate. If the specified index does not exist then this operation gives an error.
Return Value
  1. updateMany() documents return the following document:
  2. matchCount consists of the number of matched documents.
  3. modifiedCount consists of the number of modified documents.
  4. upsertedId consists of the _id for the upserted document.
  5. Acknowledged as true if the operation run with write concern. Otherwise, return false.
Examples to Understand How to Update Multiple Documents in MongoDB

In the following examples, we are going to use:
Database: dotnettutorial
Collection: course

Examples to Understand How to Update Multiple Documents in MongoDB

Updating all Documents in MongoDB

In this example, we are going to update all the documents:
db.coruse.updateMany({}, {$set:{duration: 609}})
Here, the value of the duration field is updated to 609 in all the documents present in the course field.

Updating all Documents in MongoDB

Update all the documents that match the filter in MongoDB

In the below example, we update all the documents that match the given filter:
db.coruse.updateMany({course: 90}, {$set:{duration: 800}})
Here the value of the duration field is updated to 800 whose course is 90.

Update using upsert in MongoDB

In this example, we set the value of upsert to true:
db.coruse.updateMany({name: “C#”}, {$set:{duration: 800, course: 90}}, {upsert: true})

Update using upsert in MongoDB

Here, no document matches the filter(i.e., {name: “C#”}) and the value of upsert is true. So, updateMany() function adds a new document to the course collection.

Updating Multiple Documents in MongoDB with Examples

In the next article, I am going to discuss Updating Multiple Documents in MongoDB with Examples. Here, in this article, I try to explain Updating Multiple Documents in MongoDB with Examples. I hope you enjoy this Updating Multiple Documents in MongoDB with Examples article.

Leave a Reply

Your email address will not be published. Required fields are marked *