updateMany() (Update Multiple Documents)
Last updated: 2026-10-09
updateMany() (Update Multiple Documents)
updateMany() updates every document matching a filter, e.g. `db.users.updateMany({ age: { $lt: 18 } }, { $set: { isTeen: true } })`. Unlike updateOne(), which touches only the first match, it applies the update to all matching documents — handy for batch status changes or adding fields everywhere. The result reports matchedCount (matches found) and modifiedCount (documents actually changed).
| Category | Write |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
db.<collection>.updateMany(<filter>, <update>, { upsert: <bool> })
⚙️ Parameters
| filter | Predicate object; supports query operators. |
|---|---|
| update | Object of update operators applied to every match. |
| upsert | Optional. Insert one document when nothing matches; default false. |
Returns:UpdateResult: { acknowledged, matchedCount, modifiedCount, upsertedId }.
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// updateMany: mark everyone older than 30 as senior
db.users.updateMany(
{ age: { $gt: 30 } },
{ $set: { isSenior: true } }
)
Output:
{ acknowledged: true, matchedCount: 3, modifiedCount: 3, upsertedId: null }
📌 Notes
- Updates every match; be careful with an empty filter {} which updates the whole collection
- modifiedCount can be smaller than matchedCount when values are unchanged
- On large collections, batch the work and watch performance
🏷️ Related Tags
💡 Related Cases
📚 Related Tutorials
❓ FAQ
QDoes updateMany update every matching document?
AYes, every matching document is updated. Passing an empty filter {} updates the entire collection, so be careful.
QWhat is the difference between updateMany and updateOne?
AupdateOne() updates only the first match; updateMany() updates all matches. Choose updateMany() for batch changes.
QWhy is modifiedCount smaller than matchedCount?
ASome matched documents already hold the target values, so MongoDB counts them as unchanged and excludes them from modifiedCount.