Skip to main content

What does a stage mean in aggregation?

A stage in MongoDB's aggregation is a single step in the data-processing pipeline that performs a specific action on the input documents and then passes the result to the next stage.

How to think about it

  • there's a stream of documents
  • a stage does something to that stream (filters, sorts, groups, and so on)
  • the result immediately moves on down the pipeline

Examples of stages

StageWhat it does
$matchfilters data
$groupgrouping and aggregate calculations
$sortsorting
$projectselecting and transforming fields
$limitlimiting the number of documents

A mini example

js
[ { $match: { status: "active" } }, { $sort: { created_at: -1 } } ]
  1. $match keeps only the documents you need
  2. $sort sorts the ones that are left

The main idea

A stage is a single operation inside the aggregation pipeline, and every stage has a strictly defined job to do with the data stream.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.