Aggregation Operations聚合操作
On this page本页内容
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聚合管道, which are the preferred method for performing aggregations.,这是执行聚合的首选方法。Single purpose aggregation methods单一用途聚合方法, which are simple but lack the capabilities of an aggregation pipeline.,它们很简单,但缺乏聚合管道的功能。
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开始,如果您使用使用聚合管道更新中显示的阶段,则可以使用聚合管道更新文档。
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" } }
}
] )
Filters the pizza order documents to pizzas with a将披萨订单文档筛选为中等大小的披萨。size
ofmedium
.Passes the remaining documents to the将剩余的文档传递到$group
stage.$group
阶段。
Groups the remaining documents by pizza按披萨name
.name
对剩余文档进行分组。Uses使用$sum
to calculate the total orderquantity
for each pizzaname
.$sum
计算每个披萨name
的总订单quantity
。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.有关包含示例输入文档的可运行示例,请参阅完整聚合管道示例。
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.这些方法很简单,但缺乏聚合管道的功能。
db.collection.estimatedDocumentCount() | |
db.collection.count() | |
db.collection.distinct() |