Updating Documents with updateOne() and Update Operators
MongoDB update operations can modify selected fields without replacing an entire document. Modifier operators define how the selected document changes.
Basic update syntax
db.collection.updateOne(
filter,
updateDocument,
options
)
The filter identifies the target document. The update document describes the modifications.
$set
$set creates a field if it does not exist or changes its value if it does.
db.users.updateOne(
{ name: "Arun" },
{ $set: { age: 23 } }
);
Other fields remain unchanged.
$unset
$unset removes a field from the document.
db.users.updateOne(
{ name: "Arun" },
{ $unset: { temporaryField: "" } }
);
The value supplied to $unset does not determine the resulting field value; the field is removed.
$inc
$inc increases or decreases a numeric field.
db.products.updateOne(
{ sku: "KEYBOARD-01" },
{ $inc: { stock: 5 } }
);
A negative increment decreases the field. If the field does not exist, $inc creates it with the specified value. If the field exists with a nonnumeric value, the update fails.
$mul
$mul multiplies a numeric field by a value.
db.products.updateOne(
{ sku: "MOUSE-01" },
{ $mul: { price: 1.1 } }
);
This operation changes the numeric value, but floating-point precision and the chosen numeric BSON type remain important.
$rename
$rename changes a field's name.
db.users.updateOne(
{ _id: ObjectId("65a000000000000000000001") },
{ $rename: { fullname: "name" } }
);
Renaming a field affects schema expectations, queries, and application code. Plan migrations carefully if multiple application versions are running.
$min and $max
$min updates a field only if the specified value is less than the current value under the applicable comparison rules. $max updates it only if the specified value is greater.
These operators can be useful for maintaining high-water marks or lowest observed values, but their behavior depends on type and comparison semantics.
$currentDate
$currentDate sets a field to the current date or a BSON Timestamp, depending on the requested type.
db.users.updateOne(
{ name: "Arun" },
{ $currentDate: { updatedAt: true } }
);
For application audit fields, BSON Date is usually the intended choice.
Multiple operators in one update
db.products.updateOne(
{ sku: "KEYBOARD-01" },
{
$set: { active: true },
$inc: { stock: -1 },
$currentDate: { updatedAt: true }
}
);
The update applies all specified modifications atomically to the matched document. Ensure the filter identifies the correct target and that the resulting state satisfies your business rules.
db.updateDemo.insertOne({
sku: "KEYBOARD-01",
name: "Keyboard",
stock: 10,
active: false,
obsoleteField: "remove-me"
});
db.updateDemo.updateOne(
{ sku: "KEYBOARD-01" },
{
$set: { active: true },
$inc: { stock: 5 },
$unset: { obsoleteField: "" },
$currentDate: { updatedAt: true }
}
);
db.updateDemo.findOne({ sku: "KEYBOARD-01" });- $set creates or modifies selected fields.
- $unset removes fields.
- $inc and $mul modify numeric values.
- $rename changes field names and can affect application compatibility.
- Multiple update operators can be applied atomically to one document.
What happens when `$inc` is applied to a field that does not exist?