MongoDB updateMany() Method - db.Collection.updateMany() - GeeksforGeeks

28 Jan, 2021
The updateMany() method updates all the documents in MongoDB collections that match the given query. When you update your document, the value of the _id field remains unchanged. This method can also add new fields in the document. Specify an empty document({}) in the selection criteria to update all collection documents.
This method can accept documents that only holds update operator expressions.
This method can also accept aggregation pipeline.
In this method, if the value of upsert is set to true for the shared collection, then you must include the full shared key in the filter/selection criteria.
The update operation will fail if this operation changes the size of the document.
You can also use this method inside multi-document transactions.
Syntax:
db.Collection_name.updateMany({Selection_Criteria},{$set:{Update_Data}}, 
{
    upsert: <boolean>,
    multi: <boolean>,
    writeConcern: <document>,
    collation: <document>,
    arrayFilters: [<filterdocument1>, ... ],
    hint: <document|string>        
})
Parameters:
The first parameter is the Older value in the form of Documents. Documents are a structure created of file and value pairs, similar to JSON objects. Specify an empty document ({}) to update all documents in the collection.
The second parameter must contain a $set keyword to update the following specific document value.
The third parameter is optional.
Optional Parameters:
Upsert: The default value of this parameter is false. When it is true, it will make a new document in the collection when no document matches the given condition in the update method.
Multi: The default value of this parameter is false. When it is true, the update method updates all the documents that meet the query condition. Otherwise, it will update only one document.
writeConcern: It is only used when you do not want to use the default write concern. The type of this parameter is a document.
Collation: It specifies the use of the collation for operations. It allows users to specify the language-specific rules for string comparison, like lettercase and accent marks. The type of this parameter is a document.
arrayFilters: It is an array of filter documents that indicates which array elements to modify for an update operation on an array field. The type of this parameter is an array.
Hint: It is a document or field that specifies the index to use to support the filter. It can take an index specification document or the index name string, and if you specify an index that does not exist, it will give an error.
Return:
This method returns a document that contains the following fields:
matchedCount: This field contains the number of matched documents.
modifiedCount: This field contains the number of modified documents.
upsertedId: This field contains the _id for the upserted document.
acknowledged: The value of this field is true if write concern was enabled or false if write concern was disabled.
Examples:
In the following examples, we are working with:
Database: gfg
Collection: student
Document: Three documents contains name and age of the students
db.student.updateMany({name: "aaksh"}, {$set:{age: 20}})
Here, we update the age of a student whose name is aaksh from 15 to 20 using updateMany() method.
db.student.updateMany({age:18},{$set:{eligible:"true"}})
Here, we update all the matched documents whose age is 18 to eligible: true
db.student.updateMany({age: 18}, {$set: {eligible: false}}, {upsert: true})
Here, we update all the documents that matched the given condition.
MongoDB-method
Picked
MongoDB
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to connect MongoDB with ReactJS ?
MongoDB - limit() Method
MongoDB - FindOne() Method
Create user and add role in MongoDB
MongoDB - sort() Method
MongoDB updateOne() Method - db.Collection.updateOne()
MongoDB insertMany() Method - db.Collection.insertMany()
MongoDB Cursor
MongoDB - Update() Method
How to Use Go With MongoDB?