Update Operators
Modify documents with update operators
Field Update Operators
$set
Sets the value of a field:
await users.updateOne({ _id: "abc123" }, { $set: { name: "Alice", email: "alice@example.com" } });Can set nested fields using dot notation:
await users.updateOne({ _id: "abc123" }, { $set: { "address.city": "New York" } });$unset
Removes a field from the document:
await users.updateOne({ _id: "abc123" }, { $unset: { temporaryField: "" } });The value passed to $unset doesn't matter - the field is removed regardless.
$rename
Renames a field:
await users.updateOne({ _id: "abc123" }, { $rename: { old_name: "new_name" } });Numeric Operators
$inc
Increments a field by a specified value:
// Increment by 1
await posts.updateOne({ _id: postId }, { $inc: { views: 1 } });
// Decrement by using negative value
await users.updateOne({ _id: "abc123" }, { $inc: { credits: -10 } });$mul
Multiplies a field by a specified value:
await products.updateOne(
{ _id: productId },
{ $mul: { price: 1.1 } } // 10% increase
);$min
Updates field only if the specified value is less than current:
await stats.updateOne({ _id: "daily" }, { $min: { lowestPrice: 99 } });$max
Updates field only if the specified value is greater than current:
await stats.updateOne({ _id: "daily" }, { $max: { highestPrice: 150 } });Array Operators
$push
Adds an element to an array:
await posts.updateOne(
{ _id: postId },
{ $push: { comments: { text: "Great post!", author: "Bob" } } }
);Push multiple elements with $each:
await posts.updateOne(
{ _id: postId },
{ $push: { tags: { $each: ["javascript", "tutorial", "beginner"] } } }
);$addToSet
Adds element only if it doesn't already exist:
await users.updateOne({ _id: "abc123" }, { $addToSet: { roles: "editor" } });With $each modifier:
await users.updateOne(
{ _id: "abc123" },
{ $addToSet: { permissions: { $each: ["read", "write", "delete"] } } }
);$pull
Removes all elements matching a condition:
// Remove specific value
await users.updateOne({ _id: "abc123" }, { $pull: { tags: "deprecated" } });
// Remove matching condition
await users.updateOne({ _id: "abc123" }, { $pull: { scores: { $lt: 50 } } });$pop
Removes the first or last element of an array:
// Remove last element
await queue.updateOne({ _id: "tasks" }, { $pop: { items: 1 } });
// Remove first element
await queue.updateOne({ _id: "tasks" }, { $pop: { items: -1 } });Date Operators
$currentDate
Sets field to current date:
// Set as Date
await users.updateOne({ _id: "abc123" }, { $currentDate: { lastModified: true } });
// Set as timestamp (milliseconds)
await users.updateOne(
{ _id: "abc123" },
{ $currentDate: { lastModified: { $type: "timestamp" } } }
);Combining Operators
Multiple operators can be combined in a single update:
await posts.updateOne(
{ _id: postId },
{
$set: { title: "Updated Title" },
$inc: { version: 1, views: 1 },
$push: { history: { action: "edit", date: new Date().toISOString() } },
$currentDate: { updatedAt: true },
}
);Update with Upsert
Create document if it doesn't exist:
await users.updateOne(
{ email: "new@example.com" },
{
$set: { name: "New User" },
$setOnInsert: { createdAt: new Date().toISOString() },
},
{ upsert: true }
);Always use update operators ($set, $inc, etc.) instead of passing a plain object. A plain
object will replace the entire document.