14 Topics
Chapter•28 Minutes

Cheat Sheet: Update Operators

Field, numeric, array, positional, and upsert update operators with practical use cases.

$set

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$set: practical example
db.users.updateOne({ _id: userId }, { $set: { "profile.theme": "dark" } })
💡 Explanation Sets a field without replacing the whole document. When to use it Update selected properties.
Key Takeaways
  • Sets a field without replacing the whole document.
  • Typical use: Update selected properties.

$unset

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Update operator families

What it does

Removes a field.

Syntax

{$unset: { field: "" }}

Use case

Remove deprecated properties.

$unset: practical example
db.users.updateOne({ _id: userId }, { $unset: { legacyField: "" } })
💡 Explanation Removes a field. When to use it Remove deprecated properties.
Key Takeaways
  • Removes a field.
  • Typical use: Remove deprecated properties.

$inc

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$inc: practical example
db.inventory.updateOne({ sku: "KB-1" }, { $inc: { stock: -1, sold: 1 } })
💡 Explanation Atomically increments/decrements numeric fields. When to use it Counters and inventory. Caution Use a conditional filter to prevent invalid stock.
Key Takeaways
  • Atomically increments/decrements numeric fields.
  • Typical use: Counters and inventory.

$mul

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Update operator families

What it does

Multiplies existing numeric fields by a factor.

Syntax

{$mul: { field: factor }}

Use case

Controlled scaling.

$mul: practical example
db.metrics.updateMany({}, { $mul: { score: 1.1 } })
💡 Explanation Multiplies existing numeric fields by a factor. When to use it Controlled scaling.
Key Takeaways
  • Multiplies existing numeric fields by a factor.
  • Typical use: Controlled scaling.

$min/$max

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$min/$max: practical example
db.metrics.updateOne({ _id: id }, { $max: { highScore: newScore } })
💡 Explanation Updates only when the provided value is lower or higher, respectively. When to use it Maintain high/low-water marks.
Key Takeaways
  • Updates only when the provided value is lower or higher, respectively.
  • Typical use: Maintain high/low-water marks.

$rename

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$rename: practical example
db.users.updateMany({}, { $rename: { "profile.nick": "profile.nickname" } })
💡 Explanation Renames a field. When to use it Migrate a field name. Caution Plan compatibility with old readers and validators.
Key Takeaways
  • Renames a field.
  • Typical use: Migrate a field name.

$currentDate

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$currentDate: practical example
db.orders.updateOne({ _id: orderId }, { $currentDate: { updatedAt: true } })
💡 Explanation Sets current date or a BSON timestamp with the appropriate option. When to use it Maintain modification timestamps.
Key Takeaways
  • Sets current date or a BSON timestamp with the appropriate option.
  • Typical use: Maintain modification timestamps.

$setOnInsert

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$setOnInsert: practical example
db.settings.updateOne({ _id: "theme" }, { $set: { enabled: true }, $setOnInsert: { createdAt: new Date() } }, { upsert: true })
💡 Explanation Applies values only when an upsert inserts a new document. When to use it Initialize creation metadata.
Key Takeaways
  • Applies values only when an upsert inserts a new document.
  • Typical use: Initialize creation metadata.

$push modifiers

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$push modifiers: practical example
db.users.updateOne({ _id: userId }, { $push: { recent: { $each: [entry], $sort: { at: -1 }, $slice: 10 } } })
💡 Explanation Appends values and optionally sorts, positions, or caps the array. When to use it Maintain bounded recent items.
Key Takeaways
  • Appends values and optionally sorts, positions, or caps the array.
  • Typical use: Maintain bounded recent items.

$addToSet

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$addToSet: practical example
db.users.updateOne({ _id: userId }, { $addToSet: { permissions: "reports:read" } })
💡 Explanation Adds only if an equal value is absent. When to use it Set-like roles or tags.
Key Takeaways
  • Adds only if an equal value is absent.
  • Typical use: Set-like roles or tags.