Cheat Sheet: Aggregation Pipeline Stages
Reference for common aggregation pipeline stages, syntax, behavior, and typical use cases.
$match
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: { status: "paid", total: { $gte: 1000 } } }- Filters documents using query predicate semantics.
- Typical use: Reduce downstream work; place early when semantically valid.
$project
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: { orderNumber: 1, total: 1, tax: { $multiply: ["$total", 0.18] } } }_id is a common exception.- Includes/excludes fields and computes values.
- Typical use: Shape response/report output.
$set / $addFields
Reference group: Pipeline stages
What it does
Adds or overwrites fields; $addFields aliases $set.
Syntax
{$set: { field: expression }}
Use case
Create intermediate values.
{ $set: { fullName: { $concat: ["$firstName", " ", "$lastName"] } } }$addFields aliases $set.
When to use it
Create intermediate values.- Adds or overwrites fields;
$addFieldsaliases$set. - Typical use: Create intermediate values.
$unset
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: ["internalNotes", "secretToken"] }- Removes fields from pipeline documents.
- Typical use: Strip internal fields.
$sort
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: { createdAt: -1, _id: -1 } }- Orders documents.
- Typical use: Newest-first views and deterministic pagination.
$limit
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: 20 }- Passes at most n documents onward.
- Typical use: Bound results and top-N workflows.
$skip
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: 40 }- Skips n pipeline documents.
- Typical use: Offset pagination.
$group
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: { _id: "$customerId", revenue: { $sum: "$total" }, orders: { $sum: 1 } } }_id and computes accumulators.
When to use it
Totals, counts, averages, and grouped arrays.- Groups by
_idand computes accumulators. - Typical use: Totals, counts, averages, and grouped arrays.
$unwind
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: { path: "$items", preserveNullAndEmptyArrays: false } }- Emits one pipeline document per array element.
- Typical use: Analyze individual array entries.
$lookup
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: { from: "customers", localField: "customerId", foreignField: "_id", as: "customer" } }- Joins matching documents from another collection into an array.
- Typical use: Combine related collections.