Cheat Sheet: Update Operators
Field, numeric, array, positional, and upsert update operators with practical use cases.
$set
Reference group: Update operator families
What it does
Sets a field without replacing the whole document.
Syntax
{$set: { field: value }}
Use case
Update selected properties.
db.users.updateOne({ _id: userId }, { $set: { "profile.theme": "dark" } })- Sets a field without replacing the whole document.
- Typical use: Update selected properties.
$unset
Reference group: Update operator families
What it does
Removes a field.
Syntax
{$unset: { field: "" }}
Use case
Remove deprecated properties.
db.users.updateOne({ _id: userId }, { $unset: { legacyField: "" } })- Removes a field.
- Typical use: Remove deprecated properties.
$inc
Reference group: Update operator families
What it does
Atomically increments/decrements numeric fields.
Syntax
{$inc: { field: amount }}
Use case
Counters and inventory.
Important notes
Use a conditional filter to prevent invalid stock.
db.inventory.updateOne({ sku: "KB-1" }, { $inc: { stock: -1, sold: 1 } })- Atomically increments/decrements numeric fields.
- Typical use: Counters and inventory.
$mul
Reference group: Update operator families
What it does
Multiplies existing numeric fields by a factor.
Syntax
{$mul: { field: factor }}
Use case
Controlled scaling.
db.metrics.updateMany({}, { $mul: { score: 1.1 } })- Multiplies existing numeric fields by a factor.
- Typical use: Controlled scaling.
$min/$max
Reference group: Update operator families
What it does
Updates only when the provided value is lower or higher, respectively.
Syntax
{$min: { field: value }} / {$max: { field: value }}
Use case
Maintain high/low-water marks.
db.metrics.updateOne({ _id: id }, { $max: { highScore: newScore } })- Updates only when the provided value is lower or higher, respectively.
- Typical use: Maintain high/low-water marks.
$rename
Reference group: Update operator families
What it does
Renames a field.
Syntax
{$rename: { oldName: newName }}
Use case
Migrate a field name.
Important notes
Plan compatibility with old readers and validators.
db.users.updateMany({}, { $rename: { "profile.nick": "profile.nickname" } })- Renames a field.
- Typical use: Migrate a field name.
$currentDate
Reference group: Update operator families
What it does
Sets current date or a BSON timestamp with the appropriate option.
Syntax
{$currentDate: { field: true }}
Use case
Maintain modification timestamps.
db.orders.updateOne({ _id: orderId }, { $currentDate: { updatedAt: true } })- Sets current date or a BSON timestamp with the appropriate option.
- Typical use: Maintain modification timestamps.
$setOnInsert
Reference group: Update operator families
What it does
Applies values only when an upsert inserts a new document.
Syntax
{$setOnInsert: { field: value }}
Use case
Initialize creation metadata.
db.settings.updateOne({ _id: "theme" }, { $set: { enabled: true }, $setOnInsert: { createdAt: new Date() } }, { upsert: true })- Applies values only when an upsert inserts a new document.
- Typical use: Initialize creation metadata.
$push modifiers
Reference group: Update operator families
What it does
Appends values and optionally sorts, positions, or caps the array.
Syntax
{$push: { arr: { $each, $sort?, $slice?, $position? } }}
Use case
Maintain bounded recent items.
db.users.updateOne({ _id: userId }, { $push: { recent: { $each: [entry], $sort: { at: -1 }, $slice: 10 } } })- Appends values and optionally sorts, positions, or caps the array.
- Typical use: Maintain bounded recent items.
$addToSet
Reference group: Update operator families
What it does
Adds only if an equal value is absent.
Syntax
{$addToSet: { array: value }}
Use case
Set-like roles or tags.
db.users.updateOne({ _id: userId }, { $addToSet: { permissions: "reports:read" } })- Adds only if an equal value is absent.
- Typical use: Set-like roles or tags.