Aggregation聚合

On this page本页内容

Aggregation operations process data records and return computed results.聚合操作处理数据记录并返回计算结果。Aggregation operations group values from multiple documents together, and can perform a variety of operations on the grouped data to return a single result.聚合操作将多个文档中的值组合在一起,并可以对组合的数据执行各种操作以返回单个结果。MongoDB provides three ways to perform aggregation: the aggregation pipeline, the map-reduce function, and single purpose aggregation methods.MongoDB提供了三种执行聚合的方法:聚合管道映射缩小函数单用途聚合方法

Aggregation Pipeline聚合管道

MongoDB’s aggregation framework is modeled on the concept of data processing pipelines.MongoDB的聚合框架是基于数据处理管道的概念建模的。Documents enter a multi-stage pipeline that transforms the documents into an aggregated result.文档进入一个多阶段管道,将文档转换为聚合结果。For example:例如:

In the example,在这个例子中,

db.orders.aggregate([
   { $match: { status: "A" } },
   { $group: { _id: "$cust_id", total: { $sum: "$amount" } } }
])

First Stage: The $match stage filters the documents by the status field and passes to the next stage those documents that have status equal to "A".第一阶段$match阶段按status字段过滤文档,并将status等于"A"的文档传递到下一阶段。

Second Stage: The $group stage groups the documents by the cust_id field to calculate the sum of the amount for each unique cust_id.第二阶段$group阶段按cust_id字段对文档进行分组,以计算每个唯一cust_id的金额总和。

The most basic pipeline stages provide filters that operate like queries and document transformations that modify the form of the output document.最基本的管道阶段提供了过滤器,其操作方式类似于修改输出文档形式的查询和文档转换

Other pipeline operations provide tools for grouping and sorting documents by specific field or fields as well as tools for aggregating the contents of arrays, including arrays of documents.其他管道操作提供了按特定字段对文档进行分组和排序的工具,以及聚合数组(包括文档数组)内容的工具。In addition, pipeline stages can use operators for tasks such as calculating the average or concatenating a string.此外,管道阶段可以使用运算符执行诸如计算平均值或连接字符串之类的任务。

The pipeline provides efficient data aggregation using native operations within MongoDB, and is the preferred method for data aggregation in MongoDB.管道使用MongoDB中的本机操作提供高效的数据聚合,是MongoDB中数据聚合的首选方法。

The aggregation pipeline can operate on a sharded collection.聚合管道可以对分片集合进行操作。

The aggregation pipeline can use indexes to improve its performance during some of its stages.聚合管道可以在某些阶段使用索引来提高其性能。In addition, the aggregation pipeline has an internal optimization phase.此外,聚合管道还有一个内部优化阶段。See Pipeline Operators and Indexes and Aggregation Pipeline Optimization for details.有关详细信息,请参见管道操作符和索引以及聚合管道优化

Map-Reduce映射缩小

Tip

Aggregation pipeline聚合管道 provides better performance and a more coherent interface than map-reduce.提供比映射缩小更好的性能和更连贯的接口。

For examples of aggregation alternatives to map-reduce operations, see Map-Reduce Examples.有关映射缩小操作的聚合替代方法的示例,请参见映射缩小示例See also Map-Reduce to Aggregation Pipeline.另请参阅将从映射小缩到聚合管道

MongoDB also provides map-reduce operations to perform aggregation.MongoDB还提供映射缩小操作来执行聚合。Map-reduce uses custom JavaScript functions to perform the map and reduce operations, as well as the optional finalize operation.映射缩小使用自定义JavaScript函数来执行Map和reduce操作,以及可选的finalize操作。

Diagram of the annotated map-reduce operation.

Single Purpose Aggregation Operations单一目的聚合操作

MongoDB also provides db.collection.estimatedDocumentCount(), db.collection.count() and db.collection.distinct().MongoDB还提供了db.collection.estimatedDocumentCount()db.collection.count()db.collection.distinct()

All of these operations aggregate documents from a single collection.所有这些操作都从单个集合聚合文档。While these operations provide simple access to common aggregation processes, they lack the flexibility and capabilities of the aggregation pipeline and map-reduce.虽然这些操作提供了对常见聚合过程的简单访问,但它们缺乏聚合管道和map reduce的灵活性和功能。

Diagram of the annotated distinct operation.

Additional Features and Behaviors其他特性和行为

For a feature comparison of the aggregation pipeline, map-reduce, and the special group functionality, see Aggregation Commands Comparison.有关聚合管道、map reduce和特殊组功能的功能比较,请参见聚合命令比较