Documents and MongoDB · Engineering
Aggregation pipelines: stages, memory, and limits
How NonoDB executes aggregation, which stages stream, where memory is bounded, and why a large pipeline is not a batch analytics engine.
Aggregation is the part of a document database that turns stored rows into answers. It is also the easiest way to accidentally build an unbounded analytical job on a small VM.
NonoDB implements a practical MongoDB-style pipeline with a deliberate memory boundary. Understanding the boundary is more useful than memorising the stage list.
The pipeline is read-only
A normal aggregation runs in one LMDB read snapshot and returns a result. It does not write to the collection. explain reports whether the plan used an aggregation path or a simpler find path.
The exceptions are $out and $merge, which write to same-database targets through the bucket writer. They are not read-only, but they still go through the normal writer and index maintenance.
Pushdown and streaming
A safe prefix of the pipeline is pushed into the normal find path: a leading $match, then a contiguous $sort plus $skip/$limit, a simple $project, and another $skip. That reuses secondary indexes, _id ranges, covering projections, index-only sorts, and the query-level top-K.
After the prefix, stages are evaluated in memory. A leading $group streams input into a group builder, but the group state itself is held in memory. A lone $count after $match can call the count path without materialising documents.
The pipeline is not a distributed query engine. It does not spill to disk.
The 100,000-document guardrail
Intermediate results are capped at NONODB_AGGREGATE_MAX_DOCS, default 100,000. The cap keeps a careless pipeline from allocating an unbounded result set on a small host.
The cap is a safety limit, not a tuning target. A pipeline that needs more than 100,000 intermediate documents should probably be a batch job, a different database, or a partitioned collection.
Stages that commonly surprise people
| Stage | Practical note |
|---|---|
$match |
Put it first; a leading match can use indexes. |
$project |
Simple field inclusion can be pushed down; computed expressions run later. |
$group |
Group state is in memory; high-cardinality keys are expensive. |
$sort |
A following $limit enables bounded top-K; a full sort is heavier. |
$lookup |
Equality joins, pipeline lookups, let, and nested lookups are supported; it is still a per-document lookup, not a distributed join. |
$facet |
Runs multiple sub-pipelines; each has its own memory cost. |
$setWindowFields |
Document and range windows, ranking, accumulators, $shift, $expMovingAvg, $derivative, $integral; bounded by the pipeline cap. |
$densify / $fill |
Useful for time buckets; not arbitrary range expressions. |
$graphLookup |
Depth, restriction, cycle detection; can touch many documents. |
$out / $merge |
Same-database targets, with let and whenMatched pipelines. |
The API reference has the full stage and expression list. Unsupported stages are rejected rather than silently ignored.
Cursors and aggregation
aggregate_cursor pages a large result instead of materialising the whole response. A cursor aggregation supports an optional leading $match, then a streaming prefix of $project, $addFields, $set, or $unset. Blocking stages are evaluated once, then paged from the materialised result.
That distinction matters for memory: a cursor does not make a blocking $group cheap. It only changes how the final rows are delivered.
SQL uses the same engine
SQL SELECT is translated into a normal pipeline: WHERE becomes a leading $match, GROUP BY and aggregates become $group, DISTINCT becomes a grouping stage, HAVING becomes a post-group $match, and ORDER BY/LIMIT/OFFSET become the final stages. Simple field-only selects skip aggregation and use the normal find path.
So the same memory rules apply to SQL reports. A GROUP BY with a million distinct keys is still a million-key group state in memory.
How to keep a pipeline predictable
- Start with the most selective
$match. - Keep the result set below the cap; lower the cap for constrained hosts.
- Avoid high-cardinality
$groupon a small VM. - Put
$sortbefore$limitonly when the top-K is genuinely small. - Use
explainand per-operation metrics to see the candidate count. - Use a cursor for delivery, not as a substitute for bounded computation.
Aggregation is a useful part of a small database. It is not a data warehouse. Treating it as one is the fastest way to turn a 5 MB idle process into a memory problem.