Docs HomeMongoDB Manual

Aggregation Operations聚合操作

Aggregation operations process multiple documents and return computed results. You can use aggregation operations to:聚合操作处理多个文档并返回计算结果。您可以使用聚合操作:

  • Group values from multiple documents together.将多个文档中的值组合在一起。
  • Perform operations on the grouped data to return a single result.对分组数据执行操作以返回单个结果。
  • Analyze data changes over time.分析数据随时间的变化。

To perform aggregation operations, you can use:要执行聚合操作,可以使用:

Aggregation Pipelines聚合管道

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

  • Each stage performs an operation on the input documents. 每个阶段对输入文档执行一个操作。For example, a stage can filter documents, group documents, and calculate values.例如,阶段可以筛选文档、对文档进行分组和计算值。
  • The documents that are output from a stage are passed to the next stage.从一个阶段输出的文档将传递到下一个阶段。
  • An aggregation pipeline can return results for groups of documents. 聚合管道可以返回文档组的结果。For example, return the total, average, maximum, and minimum values.例如,返回总值、平均值、最大值和最小值。

Starting in MongoDB 4.2, you can update documents with an aggregation pipeline if you use the stages shown in Updates with Aggregation Pipeline.从MongoDB 4.2开始,如果您使用使用聚合管道更新中显示的阶段,则可以使用聚合管道更新文档。

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阶段。

Aggregation Pipeline Example聚合管道示例

The following aggregation pipeline example contains two stages and returns the total order quantity of medium size pizzas grouped by pizza name:以下聚合管道示例包含两个阶段,并返回按披萨名称分组的中等大小披萨的总订单数量:

db.orders.aggregate( [

// Stage 1: Filter pizza order documents by pizza size阶段1:按披萨大小筛选披萨订单文档
{
$match: { size: "medium" }
},

// Stage 2: Group remaining documents by pizza name and calculate total quantity第2阶段:按披萨名称对剩余文档进行分组,并计算总数量
{
$group: { _id: "$name", totalQuantity: { $sum: "$quantity" } }
}

] )

The $match stage:$match阶段:

  • Filters the pizza order documents to pizzas with a size of medium.将披萨订单文档筛选为中等大小的披萨。
  • Passes the remaining documents to the $group stage.将剩余的文档传递到$group阶段。

The $group stage:$group阶段:

  • Groups the remaining documents by pizza name.按披萨name对剩余文档进行分组。
  • Uses $sum to calculate the total order quantity for each pizza name.使用$sum计算每个披萨name的总订单quantityThe total is stored in the totalQuantity field returned by the aggregation pipeline.合计存储在聚合管道返回的totalQuantity字段中。

For runnable examples containing sample input documents, see Complete Aggregation Pipeline Examples.有关包含示例输入文档的可运行示例,请参阅完整聚合管道示例

Learn More About Aggregation Pipelines了解有关聚合管道的更多信息

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

Single Purpose Aggregation Methods单用途聚合方法

The single purpose aggregation methods aggregate documents from a single collection. 单用途聚合方法聚合来自单个集合的文档。The methods are simple but lack the capabilities of an aggregation pipeline.这些方法很简单,但缺乏聚合管道的功能。

Method方法Description描述
db.collection.estimatedDocumentCount()Returns an approximate count of the documents in a collection or a view.返回集合或视图中文档的近似计数。
db.collection.count()Returns a count of the number of documents in a collection or a view.返回集合或视图中文档数的计数。
db.collection.distinct()Returns an array of documents that have distinct values for the specified field.返回指定字段具有不同值的文档数组。