Cheat Sheet: Aggregation Expressions and Accumulators
Conditional, comparison, arithmetic, object, set, accumulator, and ranking expressions used inside pipelines.
$cond
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.
{ $project: { tier: { $cond: { if: { $gte: ["$spend", 10000] }, then: "gold", else: "standard" } } } }- Chooses a result based on a boolean condition.
- Typical use: Derive flags and labels.
$switch
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.
{ $project: { band: { $switch: { branches: [{ case: { $lt: ["$score", 50] }, then: "low" }, { case: { $lt: ["$score", 80] }, then: "medium" }], default: "high" } } } }- Returns the first matching branch result.
- Typical use: Multi-range classification.
$ifNull
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.
{ $project: { label: { $ifNull: ["$nickname", "$name", "Unknown"] } } }- Returns the first non-null expression value.
- Typical use: Fallbacks for null/missing fields.
$eq/$ne/$gt/$gte/$lt/$lte
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.
{ $match: { $expr: { $gt: ["$sold", "$threshold"] } } }- Compares expression values and returns booleans.
- Typical use: Compare two fields in a document.
$and/$or/$not
Reference group: Conditional and logical expressions
What it does
Combines or negates boolean expressions.
Syntax
{$and: [expression, ...]}
Use case
Compute compound conditions.
{ $project: { eligible: { $and: [{ $gte: ["$age", 18] }, { $eq: ["$verified", true] }] } } }- Combines or negates boolean expressions.
- Typical use: Compute compound conditions.
$in
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.
{ $project: { allowed: { $in: ["$role", ["admin", "editor"]] } } }$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.- Aggregation
$inchecks whether a value is in an array. - Typical use: Evaluate membership in computed lists.
$add
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.
{ $project: { gross: { $add: ["$subtotal", "$tax", "$shipping"] } } }- Adds numeric values; date-plus-milliseconds behavior is supported in applicable cases.
- Typical use: Calculate totals or offsets.
$subtract
Reference group: Arithmetic expressions
What it does
Subtracts values; subtracting dates returns elapsed milliseconds.
Syntax
{$subtract: [expr1, expr2]}
Use case
Margins and durations.
{ $project: { margin: { $subtract: ["$revenue", "$cost"] } } }- Subtracts values; subtracting dates returns elapsed milliseconds.
- Typical use: Margins and durations.
$multiply
Reference group: Arithmetic expressions
What it does
Multiplies numeric values.
Syntax
{$multiply: [expr, ...]}
Use case
Line totals and weighted metrics.
{ $project: { lineTotal: { $multiply: ["$unitPrice", "$quantity"] } } }- Multiplies numeric values.
- Typical use: Line totals and weighted metrics.
$divide
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.
{ $project: { average: { $divide: ["$revenue", "$units"] } } }- Divides numeric expressions.
- Typical use: Ratios and averages.