Update Documents in MongoDB
From the MongoDB cheat sheet · CRUD Operations · verified Jul 2026
Update Documents
Modify existing documents in collections
javascript
// Update one document
db.users.updateOne(
{ email: "john@example.com" },
{ $set: { age: 31, modified: new Date() } }
)
// Update multiple documents
db.users.updateMany(
{ status: "inactive" },
{ $set: { status: "archived" } }
)
// Update or insert (upsert)
db.users.updateOne(
{ email: "new@example.com" },
{ $set: { name: "New User", age: 25 } },
{ upsert: true }
)🟢 Essential - Keep data up-to-date
💡 Use $set to update specific fields only
⚠️ updateMany() can affect many documents - be careful
📌 upsert: true creates doc if not found
⚡ findOneAndUpdate() returns the document
crudupdate