27 Topics
Chapter•54 Minutes

Cheat Sheet: Aggregation Expressions and Accumulators

Conditional, comparison, arithmetic, object, set, accumulator, and ranking expressions used inside pipelines.

$cond

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

Reference group: Conditional and logical expressions

What it does

Chooses a result based on a boolean condition.

Syntax

{$cond: { if, then, else }}

Use case

Derive flags and labels.

$cond: practical example
{ $project: { tier: { $cond: { if: { $gte: ["$spend", 10000] }, then: "gold", else: "standard" } } } }
💡 Explanation Chooses a result based on a boolean condition. When to use it Derive flags and labels.
Key Takeaways
  • Chooses a result based on a boolean condition.
  • Typical use: Derive flags and labels.

$switch

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

Reference group: Conditional and logical expressions

What it does

Returns the first matching branch result.

Syntax

{$switch: { branches: [{ case, then }], default }}

Use case

Multi-range classification.

$switch: practical example
{ $project: { band: { $switch: { branches: [{ case: { $lt: ["$score", 50] }, then: "low" }, { case: { $lt: ["$score", 80] }, then: "medium" }], default: "high" } } } }
💡 Explanation Returns the first matching branch result. When to use it Multi-range classification.
Key Takeaways
  • Returns the first matching branch result.
  • Typical use: Multi-range classification.

$ifNull

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

Reference group: Conditional and logical expressions

What it does

Returns the first non-null expression value.

Syntax

{$ifNull: [expression, fallback, ...]}

Use case

Fallbacks for null/missing fields.

$ifNull: practical example
{ $project: { label: { $ifNull: ["$nickname", "$name", "Unknown"] } } }
💡 Explanation Returns the first non-null expression value. When to use it Fallbacks for null/missing fields.
Key Takeaways
  • Returns the first non-null expression value.
  • Typical use: Fallbacks for null/missing fields.

$eq/$ne/$gt/$gte/$lt/$lte

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

Reference group: Conditional and logical expressions

What it does

Compares expression values and returns booleans.

Syntax

Comparison expressions such as {$gt: [a, b]}

Use case

Compare two fields in a document.

$eq/$ne/$gt/$gte/$lt/$lte: practical example
{ $match: { $expr: { $gt: ["$sold", "$threshold"] } } }
💡 Explanation Compares expression values and returns booleans. When to use it Compare two fields in a document.
Key Takeaways
  • Compares expression values and returns booleans.
  • Typical use: Compare two fields in a document.

$and/$or/$not

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

Reference group: Conditional and logical expressions

What it does

Combines or negates boolean expressions.

Syntax

{$and: [expression, ...]}

Use case

Compute compound conditions.

$and/$or/$not: practical example
{ $project: { eligible: { $and: [{ $gte: ["$age", 18] }, { $eq: ["$verified", true] }] } } }
💡 Explanation Combines or negates boolean expressions. When to use it Compute compound conditions.
Key Takeaways
  • Combines or negates boolean expressions.
  • Typical use: Compute compound conditions.

$in

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

Reference group: Conditional and logical expressions

What it does

Aggregation $in checks whether a value is in an array.

Syntax

{$in: [value, array]}

Use case

Evaluate membership in computed lists.

Important notes

Different syntax from query predicate $in.

$in: practical example
{ $project: { allowed: { $in: ["$role", ["admin", "editor"]] } } }
💡 Explanation Aggregation $in checks whether a value is in an array. When to use it Evaluate membership in computed lists. Caution Different syntax from query predicate $in.
Key Takeaways
  • Aggregation $in checks whether a value is in an array.
  • Typical use: Evaluate membership in computed lists.

$add

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

Reference group: Arithmetic expressions

What it does

Adds numeric values; date-plus-milliseconds behavior is supported in applicable cases.

Syntax

{$add: [expr, ...]}

Use case

Calculate totals or offsets.

$add: practical example
{ $project: { gross: { $add: ["$subtotal", "$tax", "$shipping"] } } }
💡 Explanation Adds numeric values; date-plus-milliseconds behavior is supported in applicable cases. When to use it Calculate totals or offsets.
Key Takeaways
  • Adds numeric values; date-plus-milliseconds behavior is supported in applicable cases.
  • Typical use: Calculate totals or offsets.

$subtract

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

Reference group: Arithmetic expressions

What it does

Subtracts values; subtracting dates returns elapsed milliseconds.

Syntax

{$subtract: [expr1, expr2]}

Use case

Margins and durations.

$subtract: practical example
{ $project: { margin: { $subtract: ["$revenue", "$cost"] } } }
💡 Explanation Subtracts values; subtracting dates returns elapsed milliseconds. When to use it Margins and durations.
Key Takeaways
  • Subtracts values; subtracting dates returns elapsed milliseconds.
  • Typical use: Margins and durations.

$multiply

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

Reference group: Arithmetic expressions

What it does

Multiplies numeric values.

Syntax

{$multiply: [expr, ...]}

Use case

Line totals and weighted metrics.

$multiply: practical example
{ $project: { lineTotal: { $multiply: ["$unitPrice", "$quantity"] } } }
💡 Explanation Multiplies numeric values. When to use it Line totals and weighted metrics.
Key Takeaways
  • Multiplies numeric values.
  • Typical use: Line totals and weighted metrics.

$divide

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

Reference group: Arithmetic expressions

What it does

Divides numeric expressions.

Syntax

{$divide: [numerator, denominator]}

Use case

Ratios and averages.

Important notes

Guard against zero/null denominators.

$divide: practical example
{ $project: { average: { $divide: ["$revenue", "$units"] } } }
💡 Explanation Divides numeric expressions. When to use it Ratios and averages. Caution Guard against zero/null denominators.
Key Takeaways
  • Divides numeric expressions.
  • Typical use: Ratios and averages.