MongoDB Field Update Operators: $set, $inc, $unset, $rename & More
A complete reference for MongoDB's field update operators — $set, $inc, $min, $max, $mul, $rename, $unset, $setOnInsert and $currentDate — with real examples.
MongoDB Field Update Operators
These operators do the actual work inside updateOne() / updateMany() — they describe how a field's value should change.
| Operator | Description |
|---|---|
$set |
Sets a field's value (creates the field if it doesn't exist) |
$unset |
Removes a field entirely |
$inc |
Increments a numeric field by a given amount |
$min |
Only updates if the new value is less than the current one |
$max |
Only updates if the new value is greater than the current one |
$mul |
Multiplies a numeric field by a given amount |
$rename |
Renames a field |
$setOnInsert |
Sets a value only when an upsert results in an insert |
$currentDate |
Sets a field to the current date or timestamp |
$set
db.employees.updateOne({ empid: 202 }, { $set: { email: 'rajangupta@gmail.com' } })
$unset
The value you give the field doesn't matter — 1 is the convention. It's a no-op (no error) if the field doesn't already exist.
db.employees.updateMany({}, { $unset: { reporting: 1 } })
db.employees.updateMany({}, { $unset: { name: 1, allmarks: 1 } }) // multiple fields at once
$inc
db.employees.updateOne({ empid: 201 }, { $inc: { salary: 5000 } }) // +5000
db.employees.updateOne({ empid: 202 }, { $inc: { salary: -1000 } }) // -1000
If the field doesn't exist, $inc creates it and sets it to the given amount.
$min / $max
db.employees.updateOne({ empid: 201 }, { $min: { salary: 45000 } })
// only applies if the current salary is greater than 45000
db.employees.updateOne({ empid: 201 }, { $max: { salary: 50000 } })
// only applies if the current salary is less than 50000
$mul
db.employees.updateOne({ empid: 202 }, { $mul: { salary: 2 } })
// doubles the salary
If the field doesn't exist yet, $mul creates it and sets it to 0.
$rename
db.employees.updateOne({ empid: 202 }, { $rename: { name: 'fname' } })
db.employees.updateMany({}, { $rename: { fname: 'name' } }) // rename it back everywhere
Silently does nothing if the field doesn't exist — no error is thrown.
$setOnInsert
Only takes effect when an upsert results in a brand-new document — it has zero effect on documents that already existed.
db.employees.updateOne(
{ empid: 210 },
{ $setOnInsert: { fathername: "Father", mothername: "Mother" } },
{ upsert: true }
)
$currentDate
Sets a field to the current date, either as a Date object or a BSON Timestamp.
db.employees.updateOne({ empid: 201 }, { $currentDate: { doj: true } })
// sets doj to a Date
db.employees.updateOne({ empid: 202 }, { $currentDate: { doj: { $type: 'timestamp' } } })
// sets doj to a Timestamp instead
Common Errors & Troubleshooting
$incon a non-numeric field —$incerrors if the field currently holds a non-numeric value (like a string); it can only increment numbers.- Combining
$setand$currentDateon the same field in one update — you can't target the same field with two different update operators in a single update document; pick one. $renamesilently doing nothing — if the source field name doesn't exist in the document,$renamejust no-ops without any error or warning — double-check for typos in the field name if a rename doesn't seem to take effect.
Key Takeaways
$set/$unsethandle adding and removing fields;$inc/$mulhandle in-place math.$min/$maxare "conditional sets" — they only write when the new value beats the old one.$setOnInsertis the only operator that behaves differently for inserts vs. updates during an upsert.