Open In App
Related Articles

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

Improve Article
Save Article
Like Article

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.




    upsert: <boolean>,

    multi: <boolean>,

    writeConcern: <document>,

    collation: <document>,

    arrayFilters: [<filterdocument1>, … ],

    hint: <document|string>        



  • 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.


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.


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


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.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 19 Sep, 2023
Like Article
Save Article
Similar Reads
Related Tutorials