Aggregation聚合

On this page本页内容

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

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 also update documents with an aggregation pipeline. 从MongoDB 4.2开始,您还可以使用聚合管道更新文档。See Updates with Aggregation Pipeline.请参阅聚合管道的更新

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
   {
      $match: { size: "medium" }
   },
   // Stage 2: Group remaining documents by pizza name and calculate total quantity
   {
      $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计算每个披萨名称的总订购量。The 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.有关包含示例输入文档的可运行示例,请参阅完整的聚合管道示例

Single Purpose Aggregation Methods单一目的聚合方法

You can use the following single purpose aggregation methods to aggregate documents from a single collection:可以使用以下单一用途聚合方法聚合单个集合中的文档:

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.返回指定字段具有不同值的文档数组。

The single purpose aggregation methods are simple but lack the capabilities of an aggregation pipeline.单一用途的聚合方法很简单,但缺乏聚合管道的功能。

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. 应该使用聚合管道,而不是map-reduceAggregation pipelines provide better performance and usability than 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, available starting in version 4.4. 对于需要自定义功能的map reduce操作,可以使用$accumulator$function聚合运算符,从版本4.4开始提供。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的聚合管道替代方案的示例,请参阅:

For a feature comparison of aggregation pipelines and map-reduce, see Aggregation Commands Comparison.有关聚合管道和map-reduce的功能比较,请参阅聚合命令比较

Learn More了解更多

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

←  Tailable CursorsAggregation Pipeline →