Database Manual / Aggregation Operations

Aggregation Pipeline聚合管道

An aggregation pipeline consists of one or more stages that process documents. 聚合管道由一个或多个处理文档的阶段组成。These documents can come from a collection, a view, or a specially designed stage.这些文档可以来自集合、视图或专门设计的阶段。

Each stage performs an operation on the input documents. For example, a stage can $filter documents, $group documents, and calculate values. The documents that a stage outputs are then passed to the next stage in the pipeline.每个阶段对输入文档执行一个操作。例如,一个阶段可以$filter文档、$group文档和计算值。一个阶段输出的文档随后被传递到管道中的下一个阶段。

An aggregation pipeline can return results for groups of documents. You can also update documents with an aggregation pipeline using the stages shown in Updates with Aggregation Pipeline.聚合管道可以返回文档组的结果。您还可以使用“使用聚合管道更新”中显示的阶段使用聚合管道来更新文档。

Note

Aggregation pipelines run with the db.collection.aggregate() method do not modify documents in a collection, unless the pipeline contains a $merge or $out stage.使用db.collection.aggregate()方法运行的聚合管道不会修改集合中的文档,除非管道包含$merge$out阶段。

You can run aggregation pipelines in the UI for deployments hosted in MongoDB Atlas.您可以在MongoDB Atlas中托管的部署的UI中运行聚合管道

When you run aggregation pipelines on MongoDB Atlas deployments in the MongoDB Atlas UI, you can preview the results at each stage.当您在MongoDB Atlas UI中对MongoDB Atlas部署运行聚合管道时,您可以在每个阶段预览结果。

Complete Aggregation Pipeline Examples完整的聚合管道示例

The Complete Aggregation Pipeline Tutorials section contains complete tutorials that provide detailed explanations of common aggregation tasks in a step-by-step format. 完整的聚合管道教程部分包含完整的教程,以分步的形式提供常见聚合任务的详细解释。The tutorials include examples for MongoDB Shell and each of the official MongoDB drivers.教程包括MongoDB Shell和每个官方MongoDB驱动程序的示例。

Additional Aggregation Pipeline Stage Details其他聚合管道阶段详细信息

An aggregation pipeline consists of one or more stages that process documents:聚合管道由一个或多个处理文档的阶段组成:

  • A stage does not have to output one document for every input document. For example, some stages may produce new documents or filter out documents.一个阶段不必为每个输入文档输出一个文档。例如,某些阶段可能会生成新文档或筛选掉文档。
  • The same stage can appear multiple times in the pipeline with these stage exceptions: $out, $merge, and $geoNear.同一阶段可以在管道中多次出现,但有以下阶段例外:$out$merge$geoNear

For all aggregation stages, see Aggregation Stages.有关所有聚合阶段,请参阅聚合阶段

Expressions and Operators表达式和运算符

Some aggregation pipeline stages accept expressions. Operators calculate values based on input expressions.一些聚合管道阶段接受表达式。运算符根据输入表达式计算值。

In the MongoDB Query Language, you can build expressions from the following components:在MongoDB查询语言中,您可以从以下组件构建表达式:

Component组件Example示例
Constants常量3
Operators运算符$add
Field path expressions字段路径表达式"$<path.to.field>"

For example, { $add: [ 3, "$inventory.total" ] } is an expression that consists of the $add operator and two operands:例如,{ $add: [ 3, "$inventory.total" ] }是一个由$add运算符和两个操作数组成的表达式:

The expression returns the result of adding 3 to the value at path inventory.total of the input document.该表达式返回将输入文档的路径inventory.total处的值加3的结果。

Field Paths字段路径

Field path expressions are used to access fields in input documents. 字段路径表达式用于访问输入文档中的字段。To specify a field path, prefix the field name or the dotted field path (if the field is in an embedded document) with a dollar sign $. 要指定字段路径,请在字段名或虚线字段路径(如果字段位于嵌入式文档中)前加上美元符号$For example, "$user" to specify the field path for the user field or "$user.name" to specify the field path to the embedded "user.name" field.例如,"$user"指定user字段的字段路径,或"$user.name"指定嵌入式"user.name"字段的字段路径。

"$<field>" is equivalent to "$$CURRENT.<field>" where the CURRENT is a system variable that defaults to the root of the current object, unless stated otherwise in specific stages.相当于"$$CURRENT.<field>",其中CURRENT是默认为当前对象根的系统变量,除非在特定阶段另有说明。

For more information and examples, see Field Paths.有关更多信息和示例,请参阅字段路径

Run an Aggregation Pipeline运行聚合管道

To run an aggregation pipeline, use:要运行聚合管道,请使用:

Update Documents Using an Aggregation Pipeline使用聚合管道更新文档

To update documents with an aggregation pipeline, use:要使用聚合管道更新文档,请使用:

Other Considerations其他注意事项

Aggregation Pipeline Limitations聚合管道限制

An aggregation pipeline has limitations on the value types and the result size. See Aggregation Pipeline Limits.聚合管道对值类型和结果大小有限制。请参见聚合管道限制

Aggregation Pipelines and Sharded Collections聚合管道和分片集合

An aggregation pipeline supports operations on sharded collections. See Aggregation Pipeline and Sharded Collections.聚合管道支持对分片集合的操作。请参见聚合管道和分片集合

Aggregation Pipelines as an Alternative to Map-Reduce聚合管道作为Map-Reduce的替代方案

Starting in MongoDB 5.0, map-reduce is deprecated:从MongoDB 5.0开始,map-reduce被弃用:

  • Instead of map-reduce, you should use an aggregation pipeline. Aggregation pipelines provide better performance and usability than map-reduce.您应该使用聚合管道而不是map-reduce。聚合管道提供了比map-reduce更好的性能和可用性。
  • You can rewrite map-reduce operations using aggregation pipeline stages, such as $group, $merge, and others.您可以使用聚合管道阶段(如$group$merge等)重写map-reduce操作。
  • For map-reduce operations that require custom functionality, you can use the $accumulator and $function aggregation operators. 对于需要自定义功能的map-reduce操作,可以使用$accumulator$function聚合运算符。You can use those operators to define custom aggregation expressions in JavaScript.您可以使用这些运算符在JavaScript中定义自定义聚合表达式。

For examples of aggregation pipeline alternatives to map-reduce, see:有关map-reduce的聚合管道替代方案的示例,请参阅:

Learn More了解更多

To learn more about aggregation pipelines, see:要了解有关聚合管道的更多信息,请参阅: