Aggregation Pipeline Limits

Aggregation operations with the aggregate command have the following limitations.

Result Size Restrictions

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

The aggregate command can return either a cursor or store the results in a collection. When returning a cursor or storing the results in a collection, each document in the result set is subject to the BSON Document Size limit, currently 16 megabytes; if any single document that exceeds the BSON Document Size limit, the command will produce 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.

Memory Restrictions

Pipeline stages have a limit of 100 MiB (100 * 1024 * 1024 bytes) of RAM. If a stage exceeds this limit, MongoDB will produce an error. To allow for the handling of large datasets, you can set the allowDiskUse option in the aggregate() method. The allowDiskUse option enables most aggregation pipeline operations to write data to a temporary file. The exceptions to the allowDiskUse option are the following aggregation operations; these operations must stay within the memory restriction limit:

  • $graphLookup stage
  • $addToSet accumulator expression used in the $group stage (Starting in version 3.6.17)
  • $push accumulator expression used in the $group stage (Starting in version 3.6.17)

If the pipeline includes other stages that observe allowDiskUse: true in the aggregate() operation, allowDiskUse: true option is in effect for these other stages.