updateOne() (Update Single)

Last updated: 2026-10-09

updateOne() (Update Single)

updateOne() updates the first document matching a filter. If multiple match, only the first (in natural order) is updated. Modifications are expressed with update operators ($set/$inc/$push...). The result includes matchedCount, modifiedCount, and upsertedId.

CategoryWrite
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

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

⚙️ Parameters

filterPredicate object matching documents; supports query operators.
updateObject of update operators, e.g. {$set: {age: 22}}.
upsertOptional. Insert a new 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):

// updateOne: set Alice's age to 22
db.users.updateOne(
  { name: "Alice" },
  { $set: { age: 22 } }
)

Output:

{ acknowledged: true, matchedCount: 1, modifiedCount: 1, upsertedId: null }

📌 Notes

📚 Related Tutorials

❓ FAQ

QWhat is the difference between updateOne and findOneAndUpdate?
AupdateOne returns only counters (matched/modifiedCount); findOneAndUpdate returns the pre- (or post-) update document, handy when you need the old/new value.
QWhat happens if updateOne matches multiple documents?
AIt updates only the first matched document. Use updateMany to update all matches; if the ordering matters, pass the sort option to control which document is treated as first.
QCan updateOne combine $set and $inc?
AYes. The update document can hold multiple operators, e.g. { $set: { status: 'active' }, $inc: { visits: 1 } }, applied atomically in order.
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%

🙏 帮我们做得更好

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

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