Cheat Sheet: Utility Operators, Variables, and BSON Types
Utility expressions, system variables, BSON type reminders, missing/null semantics, and collation.
$literal
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.
{ $project: { literalText: { $literal: "$notAField" } } }$ as a field path.
When to use it
Output strings that resemble field references.- Treats a value literally rather than interpreting a leading
$as a field path. - Typical use: Output strings that resemble field references.
$let
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.
{ $project: { total: { $let: { vars: { subtotal: { $multiply: ["$price", "$quantity"] } }, in: { $add: ["$$subtotal", "$shipping"] } } } }$$name.
When to use it
Avoid repeating complex expressions.- Defines scoped variables referenced with
$$name. - Typical use: Avoid repeating complex expressions.
$$ROOT
Reference group: Utility expressions and variables
What it does
References the full current pipeline document.
Syntax
$$ROOT
Use case
Preserve or collect complete documents.
{ $replaceWith: "$$ROOT" }- References the full current pipeline document.
- Typical use: Preserve or collect complete documents.
$$CURRENT
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.
{ $project: { currentId: "$$CURRENT._id" } }$$ROOT but can change in scoped expressions.
When to use it
Understand expression scope.- References current document context; it often starts equal to
$$ROOTbut can change in scoped expressions. - Typical use: Understand expression scope.
$$REMOVE
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.
{ $project: { optional: { $cond: [{ $eq: ["$include", true] }, "$value", "$$REMOVE"] } } }- Removes a field in supported projection/set contexts.
- Typical use: Conditionally omit fields instead of outputting null.
$$NOW
Reference group: Utility expressions and variables
What it does
Current datetime value consistent throughout a pipeline.
Syntax
$$NOW
Use case
Timestamp computed output.
{ $set: { calculatedAt: "$$NOW" } }- Current datetime value consistent throughout a pipeline.
- Typical use: Timestamp computed output.
$type/$isNumber/$isArray
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.
{ $project: { kind: { $type: "$payload" }, isArray: { $isArray: "$items" }, numeric: { $isNumber: "$price" } } }- Inspects BSON types before applying type-specific operations.
- Typical use: Audit heterogeneous data.
$mergeObjects
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.
{ $project: { settings: { $mergeObjects: [{ theme: "light", compact: false }, "$preferences"] } }- Merges objects; later non-null objects override duplicate keys.
- Typical use: Apply defaults and overrides.
$getField/$setField
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.
{ $project: { special: { $getField: { field: "price.usd", input: "$metrics" } } } }- Accesses or updates object fields, including unusual field names in supported versions.
- Typical use: Handle dynamic keys.
ObjectId
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("65a000000000000000000001")_id; includes a timestamp component.
When to use it
Document identifiers and references.
Caution
Use explicit business timestamps for business events.- A 12-byte identifier commonly used for
_id; includes a timestamp component. - Typical use: Document identifiers and references.