Aggregation Pipeline
The Aggregation Pipeline is MongoDB's framework for transforming and combining documents through a sequence of stages, conceptually similar to a SQL SELECT with GROUP BY, JOIN, and window functions. A pipeline is an array of stage operators applied in order; the output of one stage feeds the next.
Common stages
$match: filter documents (equivalent toWHERE)$project: reshape and select fields (equivalent toSELECT)$group: group and aggregate (equivalent toGROUP BY)$sort: sort documents (equivalent toORDER BY)$limit,$skip: pagination$lookup: left outer join to another collection$unwind: explode array fields into one document per element$facet: run multiple sub-pipelines in parallel on the same input$graphLookup: recursive graph traversal$merge,$out: write results to another collection
Performance considerations
- Place
$matchand$projectearly to reduce documents and fields flowing downstream. - Use indexes to support early
$matchand$sortstages. $lookupis expensive; pre-denormalising data sometimes beats join-style queries.- Use
explain()to inspect the executed plan and see which stages used indexes.
🔗
📖