MongoDB updateMany() Method (original) (raw)

Last Updated : 14 Apr, 2026

The MongoDB updateMany() method updates all documents that match a filter in a single operation, making bulk updates efficient.

**Syntax

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

Behavior of updateMany()

The behavior of updateMany() can be summarized as follows:

Examples of updateMany() in MongoDB

To understand MongoDB updateMany() we need a collection called studentson which we will perform various operations and queries.

Screenshot-2026-02-04-153411

Example 1: Update a Single Document

Update the age of the student named "Alen" to 20 using MongoDB updateMany, run the following query:

**Query:

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

**Output:

Screenshot-2026-02-04-153702

Example 2: Update Multiple Documents

To set the "eligible" field to "true" for all students whose age is 19, use the following query:

**Query:

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

**Output:

Screenshot-2026-02-04-154120

Example 3: Update 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, use the following query:

**Query:

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

**Output:

Screenshot-2026-02-04-154531

Example 4: Update with Write Concern

To update the age of all students aged 19 to 22 with a write concern that requires majority acknowledgment, use the following query:

**Query:

db.student.updateMany({age: 19 }, { $set: {age: 22 } }, { writeConcern: { w: "majority", wtimeout: 5000 }})

**Output:

Screenshot-2026-02-04-155048

Example 5: Update with Collation

To update the age of all students aged 20 to 26 with a write concern and collation settings for locale-specific rules, use the following query:

**Query:

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

**Output:

image