Database Manual / Aggregation Operations / Aggregation Pipeline

Aggregation Pipeline Limits聚合管道限制

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. aggregate命令可以返回游标或将结果存储在集合中。结果集中的每个文档都受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阶段数量限制

MongoDB limits the number of aggregation pipeline stages allowed in a single pipeline to 1000.MongoDB将单个管道中允许的聚合管道阶段的数量限制为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.从MongoDB 6.0开始,allowDiskUseByDefault参数控制默认情况下是否需要超过100兆字节内存来执行将临时文件写入磁盘的流水线阶段。

  • 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. You can enable writing temporary files to disk for specific find or aggregate using the { allowDiskUse: true } option.如果allowDiskUseByDefault设置为false,则默认情况下,需要超过100兆字节内存才能执行的管道阶段会引发错误。您可以使用{ 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 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.某些阶段在处理完所有传入文档之前无法输出任何文档。这些流水线阶段必须将其阶段输出保存在RAM中,直到处理完所有传入文档。因此,这些流水线阶段可能需要比100MB限制更多的空间。

If the results of one of your $sort pipeline stages exceed the limit, consider adding a $limit stage.如果$sort管道阶段之一的结果超过了限制,请考虑添加一个$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.如果由于内存限制,任何聚合阶段将数据写入临时文件,分析器日志消息诊断日志消息都会包含一个usedDisk指示符。