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): Iftrue
, creates a new document if no documents match the filter. The default isfalse
.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
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.