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 从MongoDB 6.0开始,allowDiskUseByDefault parameter controls whether pipeline stages that require more than 100 megabytes of memory to execute write temporary files to disk by default.allowDiskUseByDefault参数控制默认情况下是否需要超过100兆字节内存来执行将临时文件写入磁盘的流水线阶段。
If如果allowDiskUseByDefaultis set totrue, 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您可以使用findoraggregatecommands using the{ allowDiskUse: false }option.{ allowDiskUse: false }选项禁用将特定find或aggregate命令的临时文件写入磁盘。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.allowDiskUseByDefault设置为false,则默认情况下,需要超过100兆字节内存才能执行的管道阶段会引发错误。您可以使用{ allowDiskUse: true }选项启用将临时文件写入磁盘以进行特定find或aggregate。
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:allowDiskUse为true时,可以将临时文件写入磁盘的阶段示例包括:
$bucket$bucketAuto$group$setWindowFields$sortwhen the sort operation is not supported by an index当索引不支持排序操作时$sortByCount
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指示符。