MongoDB updateMany() Method – db.Collection.updateMany()

MongoDB updateMany method is a powerful feature used to update multiple documents in a collection that match a specified filter. This method allows us to apply updates to all documents that meet the criteria defined in the filter.

By using MongoDB updateMany developers can efficiently perform bulk update operations. In this article, We will learn about MongoDB updateMany with examples in detail.

MongoDB updateMany()

  • The MongoDB updateMany is used to update multiple documents in a collection that match a specified filter. This method allows us to apply updates to all documents that meet the criteria defined in the filter.
  • In this method, if the value of upsert is set to true for the sharded collection, then we must include the full shard key in the filter/selection criteria.
  • This method can also accept aggregation pipelines.
  • The update operation will fail if this operation changes the size of the document.
  • We can also use this method inside multi-document transactions.

Syntax:

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

Explanation:

  • filter: A query that matches the documents to update.
  • update: The modifications to apply.
  • upsert (optional): If true, creates a new document if no documents match the filter. The default is false.
  • writeConcern (optional): A document expressing the write concern.
  • collation (optional): Specifies the collation to use for the operation.
  • arrayFilters (optional): An array of filter documents that determines which array elements to modify for an update operation on an array field.
  • hint (optional): The index to use for the query

Behavior

  • The MongoDB updateMany updates all documents that match the specified filter criteria. This method performs a bulk update operation, applying the specified update to each document that meets the filter conditions.
  • It provides the ability to update multiple documents in a single command, which can significantly improve performance and reduce network overhead compared to individual updates.
  • The operation is atomic for each document, ensuring data integrity. MongoDB updateMany supports various options, including write concerns and collation, allowing for customized and reliable updates that adhere to localespecific rules and acknowledgment requirements from replica set members.

Examples

To understand MongoDB updateMany we need a collection called students on which we will perform various operations and queries.

1. Update single document

Let’s Update the age of the student named “aaksh” to 20 using MongoDB updateMany.

db.student.updateMany({name: "aaksh"}, {$set:{age: 20}})

Output:

2. Update Multiple Documents

Set the “eligible” field to “true” for all students whose age is 18.

db.student.updateMany({age:18},{$set:{eligible:"true"}})

Output:

3. Update Multiple Documents with Upsert

Let’s Update all documents matching the condition to set “eligible” to false and create a new document if no match is found.

db.student.updateMany({age: 18}, {$set: {eligible: false}}, {upsert: true})

Output:

4. Update with Write Concern

Let’s Update the age of all students aged 18 to 20 with a write concern that requires majority acknowledgment.

db.student.updateMany(
{ "age": 18 },
{ $set: { "age": 20 } },
{ writeConcern: { w: "majority", wtimeout: 5000 } }
)
db.student.find().pretty()

Output:

{
"_id": ObjectId("600ebc010cf217478ba93570"),
"name": "aaksh",
"age": 15
}
{
"_id": ObjectId("600ebc010cf217478ba93571"),
"name": "nikhil",
"age": 20
}
{
"_id": ObjectId("600ebc010cf217478ba93572"),
"name": "vishal",
"age": 20
}

5. Specify Collation

Let’s Update the age of all students aged 18 to 20 with a write concern and collation settings for locale-specific rules.

db.student.updateMany(
{ "age": 18 },
{ $set: { "age": 20 } },
{
writeConcern: { w: "majority", wtimeout: 5000 },
collation: { locale: "en", strength: 2 }
}
)

db.student.find().pretty()

Output:

{
"_id": ObjectId("600ebc010cf217478ba93570"),
"name": "aaksh",
"age": 15
}
{
"_id": ObjectId("600ebc010cf217478ba93571"),
"name": "nikhil",
"age": 20
}
{
"_id": ObjectId("600ebc010cf217478ba93572"),
"name": "vishal",
"age": 20
}

Conclusion

Overall, MongoDB updateMany method in MongoDB is a fast and powerful tool for performing bulk updates on multiple documents that match a specified filter. It offers various options such as write concerns and collation to customize and ensure the reliability of the update operations.



Contact Us