Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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 sharded collection, then you must include the full shard 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:

  • Filter : The selection criteria for the update. Specify an empty document ({}) to update all documents in the collection.
  • update : A document or pipeline that contains modifications to apply.
  • options : 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 Type:

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

Update single document

For Updating only a single document one should use ‘updateOne()’ method can also be used instead of ‘updateMany()’,

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

Here in reference perameter ‘name’ is used which can be same for multiple users. In this case ‘updateMany()’ will change( or Update) the data for all users, while ‘updateOne’ will change the data for only one user.

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 updateOne() method.

Update multiple documents

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

Here, we update all the matched documents whose age is 18 to eligible: true

Update document with upsert

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

Here, we update all the documents that matched the given condition.


Last Updated : 05 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads