30 Topics
Chapter•60 Minutes

Cheat Sheet: Utility Operators, Variables, and BSON Types

Utility expressions, system variables, BSON type reminders, missing/null semantics, and collation.

$literal

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

Reference group: Utility expressions and variables

What it does

Treats a value literally rather than interpreting a leading $ as a field path.

Syntax

{$literal: value}

Use case

Output strings that resemble field references.

$literal: practical example
{ $project: { literalText: { $literal: "$notAField" } } }
💡 Explanation Treats a value literally rather than interpreting a leading $ as a field path. When to use it Output strings that resemble field references.
Key Takeaways
  • Treats a value literally rather than interpreting a leading $ as a field path.
  • Typical use: Output strings that resemble field references.

$let

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

Reference group: Utility expressions and variables

What it does

Defines scoped variables referenced with $$name.

Syntax

{$let: { vars: { name: expression }, in: expression }}

Use case

Avoid repeating complex expressions.

$let: practical example
{ $project: { total: { $let: { vars: { subtotal: { $multiply: ["$price", "$quantity"] } }, in: { $add: ["$$subtotal", "$shipping"] } } } }
💡 Explanation Defines scoped variables referenced with $$name. When to use it Avoid repeating complex expressions.
Key Takeaways
  • Defines scoped variables referenced with $$name.
  • Typical use: Avoid repeating complex expressions.

$$ROOT

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

Reference group: Utility expressions and variables

What it does

References the full current pipeline document.

Syntax

$$ROOT

Use case

Preserve or collect complete documents.

$$ROOT: practical example
{ $replaceWith: "$$ROOT" }
💡 Explanation References the full current pipeline document. When to use it Preserve or collect complete documents.
Key Takeaways
  • References the full current pipeline document.
  • Typical use: Preserve or collect complete documents.

$$CURRENT

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

Reference group: Utility expressions and variables

What it does

References current document context; it often starts equal to $$ROOT but can change in scoped expressions.

Syntax

$$CURRENT

Use case

Understand expression scope.

$$CURRENT: practical example
{ $project: { currentId: "$$CURRENT._id" } }
💡 Explanation References current document context; it often starts equal to $$ROOT but can change in scoped expressions. When to use it Understand expression scope.
Key Takeaways
  • References current document context; it often starts equal to $$ROOT but can change in scoped expressions.
  • Typical use: Understand expression scope.

$$REMOVE

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

Reference group: Utility expressions and variables

What it does

Removes a field in supported projection/set contexts.

Syntax

$$REMOVE

Use case

Conditionally omit fields instead of outputting null.

$$REMOVE: practical example
{ $project: { optional: { $cond: [{ $eq: ["$include", true] }, "$value", "$$REMOVE"] } } }
💡 Explanation Removes a field in supported projection/set contexts. When to use it Conditionally omit fields instead of outputting null.
Key Takeaways
  • Removes a field in supported projection/set contexts.
  • Typical use: Conditionally omit fields instead of outputting null.

$$NOW

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

Reference group: Utility expressions and variables

What it does

Current datetime value consistent throughout a pipeline.

Syntax

$$NOW

Use case

Timestamp computed output.

$$NOW: practical example
{ $set: { calculatedAt: "$$NOW" } }
💡 Explanation Current datetime value consistent throughout a pipeline. When to use it Timestamp computed output.
Key Takeaways
  • Current datetime value consistent throughout a pipeline.
  • Typical use: Timestamp computed output.

$type/$isNumber/$isArray

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

Reference group: Utility expressions and variables

What it does

Inspects BSON types before applying type-specific operations.

Syntax

Type inspection expressions

Use case

Audit heterogeneous data.

$type/$isNumber/$isArray: practical example
{ $project: { kind: { $type: "$payload" }, isArray: { $isArray: "$items" }, numeric: { $isNumber: "$price" } } }
💡 Explanation Inspects BSON types before applying type-specific operations. When to use it Audit heterogeneous data.
Key Takeaways
  • Inspects BSON types before applying type-specific operations.
  • Typical use: Audit heterogeneous data.

$mergeObjects

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

Reference group: Utility expressions and variables

What it does

Merges objects; later non-null objects override duplicate keys.

Syntax

{$mergeObjects: [object1, object2, ...]}

Use case

Apply defaults and overrides.

$mergeObjects: practical example
{ $project: { settings: { $mergeObjects: [{ theme: "light", compact: false }, "$preferences"] } }
💡 Explanation Merges objects; later non-null objects override duplicate keys. When to use it Apply defaults and overrides.
Key Takeaways
  • Merges objects; later non-null objects override duplicate keys.
  • Typical use: Apply defaults and overrides.

$getField/$setField

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

Reference group: Utility expressions and variables

What it does

Accesses or updates object fields, including unusual field names in supported versions.

Syntax

Object field access/update expressions

Use case

Handle dynamic keys.

$getField/$setField: practical example
{ $project: { special: { $getField: { field: "price.usd", input: "$metrics" } } } }
💡 Explanation Accesses or updates object fields, including unusual field names in supported versions. When to use it Handle dynamic keys.
Key Takeaways
  • Accesses or updates object fields, including unusual field names in supported versions.
  • Typical use: Handle dynamic keys.

ObjectId

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

Reference group: BSON types and field properties

What it does

A 12-byte identifier commonly used for _id; includes a timestamp component.

Syntax

ObjectId("24-hex-character-string")

Use case

Document identifiers and references.

Important notes

Use explicit business timestamps for business events.

ObjectId: practical example
ObjectId("65a000000000000000000001")
💡 Explanation A 12-byte identifier commonly used for _id; includes a timestamp component. When to use it Document identifiers and references. Caution Use explicit business timestamps for business events.
Key Takeaways
  • A 12-byte identifier commonly used for _id; includes a timestamp component.
  • Typical use: Document identifiers and references.