Aggregation Pipeline Limits

Aggregation operations with the aggregate command have thefollowing limitations.

Result Size Restrictions

Changed in version 3.6: MongoDB 3.6 removes the option for the aggregatecommand to return its results as a single document.

The aggregate command can returneither a cursor or store the results in a collection. When returning acursor or storing the results in a collection, each document in theresult set is subject to the BSON Document Size limit,currently 16 megabytes; if any single document that exceeds theBSON Document Size limit, the command will produce an error.The limit only applies to the returned documents; during the pipelineprocessing, the documents may exceed this size. Thedb.collection.aggregate() method returns a cursor by default.

Memory Restrictions

Pipeline stages have a limit of 100 megabytes of RAM. If a stageexceeds this limit, MongoDB will produce an error. To allow for thehandling of large datasets, use the allowDiskUse option to enableaggregation pipeline stages to write data to temporary files.

Changed in version 3.4.

The $graphLookup stage must stay within the 100 megabytememory limit. If allowDiskUse: true is specified for theaggregate() operation, the$graphLookup stage ignores the option. If there are otherstages in the aggregate() operation,allowDiskUse: true option is in effect for these other stages.

Starting in MongoDB 4.2, the profiler log messages and diagnostic logmessages includes a usedDiskindicator if any aggregation stage wrote data to temporary files dueto memory restrictions.

See also

$sort and Memory Restrictions and $group Operator and Memory.