Aggregation operations with the aggregate command have the
following limitations.
Result Size Restrictions
The aggregate command can either return a cursor or store
the results in a collection. Each document in the result set is subject
to the 16 mebibyte BSON Document Size limit. If any single document exceeds the BSON Document Size
limit, the aggregation produces an error. The
limit only applies to the returned documents. During the pipeline
processing, the documents may exceed this size. The
db.collection.aggregate() method returns a cursor by default.
Number of Stages Restrictions
MongoDB limits the number of aggregation pipeline stages allowed in a single pipeline to 1000.
If an aggregation pipeline exceeds the stage limit before or after being parsed, you receive an error.
Memory Restrictions
Starting in MongoDB 6.0, the allowDiskUseByDefault
parameter controls whether pipeline stages that require more than 100
megabytes of memory to execute write temporary files to disk by
default.
If
allowDiskUseByDefaultis set totrue, pipeline stages that require more than 100 megabytes of memory to execute write temporary files to disk by default. You can disable writing temporary files to disk for specificfindoraggregatecommands using the{ allowDiskUse: false }option.If
allowDiskUseByDefaultis set tofalse, pipeline stages that require more than 100 megabytes of memory to execute raise an error by default. You can enable writing temporary files to disk for specificfindoraggregateusing the{ allowDiskUse: true }option.
The $search aggregation stage is not restricted to
100 megabytes of RAM because it runs in a separate process.
Examples of stages that can write temporary files to disk when
allowDiskUse is true are:
$sortwhen the sort operation is not supported by an index
Note
Pipeline stages operate on streams of documents with each pipeline stage taking in documents, processing them, and then outputting the resulting documents.
Some stages can't output any documents until they have processed all incoming documents. These pipeline stages must keep their stage output in RAM until all incoming documents are processed. As a result, these pipeline stages may require more space than the 100 MB limit.
If the results of one of your $sort pipeline stages exceed
the limit, consider adding a $limit stage.
In addition to the per-stage limit, a single operation can use no more memory than the per-operation memory limit.
The profiler log messages and diagnostic log
messages includes a usedDisk
indicator if any aggregation stage wrote data to temporary files due
to memory restrictions.
Per-Operation Memory Limit
Starting in MongoDB 9.0, MongoDB limits the total amount of memory
that a single query operation can use. The limit applies to the
combined memory of all memory-tracked stages of the operation.
Tracked memory includes blocking stages such as $group
and $sort, and the evaluation of expressions.
By default, the limit is 1 gigabyte or 20% of the memory available to the server process, whichever is greater. MongoDB sets the default when the server starts.
The per-operation limit is separate from the 100 megabyte per-stage memory limit and doesn't change how stages spill to disk. To learn about the per-stage limit, see Memory Restrictions.
If an operation exceeds the per-operation limit, MongoDB ends the operation and returns one of the following errors:
QueryExceededMemoryLimitNoDiskUseAllowedwhen a stage that can spill to disk exceeds the limit andallowDiskUseisfalse. Run the operation with{ allowDiskUse: true }so that the stage spills to disk instead of failing.ExceededMemoryLimitfor other operations that exceed the limit.
Most operations use much less memory than the limit. If an operation returns a memory limit error, reduce the memory that the operation requires by performing one or more of the following actions:
Use an index to satisfy the sort so that MongoDB doesn't sort documents in memory. To learn more, see Sort and Index Use.
Add a
$limitstage after a$sortstage so that the sort keeps only the documents you need. To learn more, see $sort and $limit Memory Optimization.Filter documents with
$matchas early as possible in the pipeline so that later stages process fewer documents. To learn more, see Aggregation Pipeline Optimization.
To check per-operation memory usage, use these
serverStatus metrics: