Docs HomeMongoDB Manual

Aggregation Pipeline Limits聚合管道限制

Aggregation operations with the aggregate command have the following limitations.使用aggregate命令的聚合操作具有以下限制。

Result Size Restrictions结果大小限制

The aggregate command can either return a cursor or store the results in a collection. aggregate命令可以返回游标,也可以将结果存储在集合中。Each document in the result set is subject to the 16 megabyte BSON Document Size limit. 结果集中的每个文档都受16兆字节BSON文档大小限制If any single document exceeds the BSON Document Size limit, the aggregation produces an error. 如果任何单个文档超过BSON文档大小限制,则聚合会产生错误。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.db.collection.aggregate()方法默认返回一个游标。

Number of Stages Restrictions阶段数限制

Changed in version 5.05.0版更改: MongoDB 5.0 limits the number of aggregation pipeline stages allowed in a single pipeline to 1000.:MongoDB 5.0将单个管道中允许的聚合管道阶段数限制为1000。

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.从MongoDB 6.0开始,allowDiskUseByDefault参数控制需要超过100MB内存才能执行的管道阶段是否默认将临时文件写入磁盘。

  • If allowDiskUseByDefault is set to true, pipeline stages that require more than 100 megabytes of memory to execute write temporary files to disk by default. 如果allowDiskUseByDefault设置为true,则默认情况下,需要超过100兆字节内存才能执行的管道阶段会将临时文件写入磁盘。You can disable writing temporary files to disk for specific find or aggregate commands using the { allowDiskUse: false } option.您可以使用{ allowDiskUse: false }选项禁用将临时文件写入磁盘以执行特定的findaggregate命令。
  • If allowDiskUseByDefault is set to false, pipeline stages that require more than 100 megabytes of memory to execute raise an error by default. 如果allowDiskUseByDefault设置为false,则需要超过100兆字节内存才能执行的管道阶段在默认情况下会引发错误。You can enable writing temporary files to disk for specific find or aggregate using the { allowDiskUse: true } option.您可以使用{ allowDiskUse: true }选项将临时文件写入磁盘以进行特定findaggregate

The $search aggregation stage is not restricted to 100 megabytes of RAM because it runs in a separate process.$search聚合阶段不限于100兆字节的RAM,因为它在一个单独的进程中运行。

Examples of stages that can write temporary files to disk when allowDiskUse is true are:allowDiskUsetrue时,可以将临时文件写入磁盘的阶段示例如下:

Note

Pipeline stages operate on streams of documents with each pipeline stage taking in documents, processing them, and then outputing 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. 某些阶段在处理完所有传入文档后才能输出任何文档。这些流水线阶段必须将其阶段输出保存在RAM中,直到处理完所有传入的文档。As a result, these pipeline stages may require more space than the 100 MB limit.因此,这些流水线阶段可能需要比100MB限制更多的空间。

If the results of one of your $sort pipeline stages exceed the limit, consider adding a $limit stage.如果某个$sort管道阶段的结果超过了限制,请考虑添加一个$limit阶段

Starting in MongoDB 4.2, 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.从MongoDB 4.2开始,如果任何聚合阶段由于内存限制将数据写入临时文件,探查器日志消息诊断日志消息都会包含一个usedDisk指示符。