27 Topics
Chapter•54 Minutes

Cheat Sheet: Aggregation Pipeline Stages

Reference for common aggregation pipeline stages, syntax, behavior, and typical use cases.

$match

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

Reference group: Pipeline stages

What it does

Filters documents using query predicate semantics.

Syntax

{$match: filter}

Use case

Reduce downstream work; place early when semantically valid.

$match: practical example
{ $match: { status: "paid", total: { $gte: 1000 } } }
💡 Explanation Filters documents using query predicate semantics. When to use it Reduce downstream work; place early when semantically valid.
Key Takeaways
  • Filters documents using query predicate semantics.
  • Typical use: Reduce downstream work; place early when semantically valid.

$project

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

Reference group: Pipeline stages

What it does

Includes/excludes fields and computes values.

Syntax

{$project: { field: 1, computed: expression }}

Use case

Shape response/report output.

Important notes

Inclusion/exclusion combinations have rules; _id is a common exception.

$project: practical example
{ $project: { orderNumber: 1, total: 1, tax: { $multiply: ["$total", 0.18] } } }
💡 Explanation Includes/excludes fields and computes values. When to use it Shape response/report output. Caution Inclusion/exclusion combinations have rules; _id is a common exception.
Key Takeaways
  • Includes/excludes fields and computes values.
  • Typical use: Shape response/report output.

$set / $addFields

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

Reference group: Pipeline stages

What it does

Adds or overwrites fields; $addFields aliases $set.

Syntax

{$set: { field: expression }}

Use case

Create intermediate values.

$set / $addFields: practical example
{ $set: { fullName: { $concat: ["$firstName", " ", "$lastName"] } } }
💡 Explanation Adds or overwrites fields; $addFields aliases $set. When to use it Create intermediate values.
Key Takeaways
  • Adds or overwrites fields; $addFields aliases $set.
  • Typical use: Create intermediate values.

$unset

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

Reference group: Pipeline stages

What it does

Removes fields from pipeline documents.

Syntax

{$unset: "field"} or {$unset: ["a", "b"]}

Use case

Strip internal fields.

$unset: practical example
{ $unset: ["internalNotes", "secretToken"] }
💡 Explanation Removes fields from pipeline documents. When to use it Strip internal fields.
Key Takeaways
  • Removes fields from pipeline documents.
  • Typical use: Strip internal fields.

$sort

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

Reference group: Pipeline stages

What it does

Orders documents.

Syntax

{$sort: { field: 1 or -1 }}

Use case

Newest-first views and deterministic pagination.

Important notes

Use a unique tie-breaker when stable order matters.

$sort: practical example
{ $sort: { createdAt: -1, _id: -1 } }
💡 Explanation Orders documents. When to use it Newest-first views and deterministic pagination. Caution Use a unique tie-breaker when stable order matters.
Key Takeaways
  • Orders documents.
  • Typical use: Newest-first views and deterministic pagination.

$limit

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

Reference group: Pipeline stages

What it does

Passes at most n documents onward.

Syntax

{$limit: n}

Use case

Bound results and top-N workflows.

$limit: practical example
{ $limit: 20 }
💡 Explanation Passes at most n documents onward. When to use it Bound results and top-N workflows.
Key Takeaways
  • Passes at most n documents onward.
  • Typical use: Bound results and top-N workflows.

$skip

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

Reference group: Pipeline stages

What it does

Skips n pipeline documents.

Syntax

{$skip: n}

Use case

Offset pagination.

Important notes

Large offsets can be expensive.

$skip: practical example
{ $skip: 40 }
💡 Explanation Skips n pipeline documents. When to use it Offset pagination. Caution Large offsets can be expensive.
Key Takeaways
  • Skips n pipeline documents.
  • Typical use: Offset pagination.

$group

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

Reference group: Pipeline stages

What it does

Groups by _id and computes accumulators.

Syntax

{$group: { _id: expression, field: accumulator }}

Use case

Totals, counts, averages, and grouped arrays.

$group: practical example
{ $group: { _id: "$customerId", revenue: { $sum: "$total" }, orders: { $sum: 1 } } }
💡 Explanation Groups by _id and computes accumulators. When to use it Totals, counts, averages, and grouped arrays.
Key Takeaways
  • Groups by _id and computes accumulators.
  • Typical use: Totals, counts, averages, and grouped arrays.

$unwind

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

Reference group: Pipeline stages

What it does

Emits one pipeline document per array element.

Syntax

{$unwind: { path: "$array", preserveNullAndEmptyArrays? }}

Use case

Analyze individual array entries.

$unwind: practical example
{ $unwind: { path: "$items", preserveNullAndEmptyArrays: false } }
💡 Explanation Emits one pipeline document per array element. When to use it Analyze individual array entries.
Key Takeaways
  • Emits one pipeline document per array element.
  • Typical use: Analyze individual array entries.

$lookup

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

Reference group: Pipeline stages

What it does

Joins matching documents from another collection into an array.

Syntax

{$lookup: { from, localField, foreignField, as }}

Use case

Combine related collections.

$lookup: practical example
{ $lookup: { from: "customers", localField: "customerId", foreignField: "_id", as: "customer" } }
💡 Explanation Joins matching documents from another collection into an array. When to use it Combine related collections.
Key Takeaways
  • Joins matching documents from another collection into an array.
  • Typical use: Combine related collections.