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

CategoryWrite
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

db.<collection>.updateMany(<filter>, <update>, { upsert: <bool> })

⚙️ Parameters

filterPredicate object; supports query operators.
updateObject of update operators applied to every match.
upsertOptional. 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

📚 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏