On this page本页内容
An aggregation pipeline consists of one or more stages that process documents:聚合管道由一个或多个处理文档的阶段组成:
Starting in MongoDB 4.2, you can also update documents with an aggregation pipeline. 从MongoDB 4.2开始,您还可以使用聚合管道更新文档。See Updates with Aggregation Pipeline.请参阅使用聚合管道的更新。
This section shows aggregation pipeline examples that use the following pizza 本节显示了使用以下pizza orders
collection:orders
集合的聚合管道示例:
db.orders.insertMany( [ { _id: 0, name: "Pepperoni", size: "small", price: 19, quantity: 10, date: ISODate( "2021-03-13T08:14:30Z" ) }, { _id: 1, name: "Pepperoni", size: "medium", price: 20, quantity: 20, date : ISODate( "2021-03-13T09:13:24Z" ) }, { _id: 2, name: "Pepperoni", size: "large", price: 21, quantity: 30, date : ISODate( "2021-03-17T09:22:12Z" ) }, { _id: 3, name: "Cheese", size: "small", price: 12, quantity: 15, date : ISODate( "2021-03-13T11:21:39.736Z" ) }, { _id: 4, name: "Cheese", size: "medium", price: 13, quantity:50, date : ISODate( "2022-01-12T21:23:13.331Z" ) }, { _id: 5, name: "Cheese", size: "large", price: 14, quantity: 10, date : ISODate( "2022-01-12T05:08:13Z" ) }, { _id: 6, name: "Vegan", size: "small", price: 17, quantity: 10, date : ISODate( "2021-01-13T05:08:13Z" ) }, { _id: 7, name: "Vegan", size: "medium", price: 18, quantity: 10, date : ISODate( "2021-01-13T05:10:13Z" ) } ] )
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" } } } ] )
size
of medium
.$group
stage.$group
阶段。name
.$sum
to calculate the total order quantity
for each pizza name
. $sum
计算每个披萨名称的总订购量quantity
。totalQuantity
field returned by the aggregation pipeline.totalQuantity
字段中。Example output:示例输出:
[ { _id: 'Cheese', totalQuantity: 50 }, { _id: 'Vegan', totalQuantity: 10 }, { _id: 'Pepperoni', totalQuantity: 20 } ]
The following example calculates the total pizza order value and average order quantity between two dates:以下示例计算两个日期之间的披萨订单总值和平均订单量:
db.orders.aggregate( [ //Stage 1: Filter pizza order documents by date range阶段1:按日期范围筛选披萨订单文档 { $match: { "date": { $gte: new ISODate( "2020-01-30" ), $lt: new ISODate( "2022-01-30" ) } } }, //Stage 2: Group remaining documents by date and calculate results第2阶段:按日期将剩余文档分组并计算结果 { $group: { _id: { $dateToString: { format: "%Y-%m-%d", date: "$date" } }, totalOrderValue: { $sum: { $multiply: [ "$price", "$quantity" ] } }, averageOrderQuantity: { $avg: "$quantity" } } }, //Stage 3: Sort documents by totalOrderValue in descending order第3阶段:按totalOrderValue降序排列文档 { $sort: { totalOrderValue: -1 } } ] )
$gte
and $lt
.$gte
和$lt
指定的日期范围内的文档。$group
stage.$group
阶段。$dateToString
.$dateToString
按日期对文档进行分组。For each group, calculates:对于每组,计算:
$sort
stage.$sort
阶段。-1
).-1
)对文档进行排序。Example output:示例输出:
[ { _id: '2022-01-12', totalOrderValue: 790, averageOrderQuantity: 30 }, { _id: '2021-03-13', totalOrderValue: 770, averageOrderQuantity: 15 }, { _id: '2021-03-17', totalOrderValue: 630, averageOrderQuantity: 30 }, { _id: '2021-01-13', totalOrderValue: 350, averageOrderQuantity: 10 } ]
An aggregation pipeline consists of one or more stages that process documents:聚合管道由一个或多个处理文档的阶段组成:
$out
, $merge
, and $geoNear
.$out
、$merge
和$geoNear
。For all aggregation stages, see Aggregation Pipeline Stages.有关所有聚合阶段,请参阅聚合管道阶段。
Some aggregation pipeline stages accept an aggregation expression, which:某些聚合管道阶段接受聚合表达式,该表达式:
Starting in MongoDB 4.4, you can use the 从MongoDB 4.4开始,您可以使用$accumulator
and $function
aggregation operators to define custom aggregation expressions in JavaScript.$accumulator
和$function
聚合运算符在JavaScript中定义自定义聚合表达式。
For all aggregation expressions, see Expressions.有关所有聚合表达式,请参阅表达式。
To run an aggregation pipeline, use:要运行聚合管道,请使用:
To update documents with an aggregation pipeline, use:要使用聚合管道更新文档,请使用:
mongosh | |
---|---|
findAndModify | |
update |
An aggregation pipeline has limitations on the value types and the result size. 聚合管道对值类型和结果大小有限制。See Aggregation Pipeline Limits.请参阅聚合管道限制。
An aggregation pipeline supports operations on sharded collections. 聚合管道支持对分片集合的操作。See Aggregation Pipeline and Sharded Collections.请参阅聚合管道和分片集合。
Starting in MongoDB 5.0, map-reduce is deprecated:从MongoDB 5.0开始,不推荐使用map-reduce:
$group
, $merge
, and others.$group
、$merge
等)重写map-reduce操作。$accumulator
and $function
aggregation operators, available starting in version 4.4. $accumulator
和$function
聚合运算符,从版本4.4开始提供。For examples of aggregation pipeline alternatives to map-reduce, see:有关map reduce的聚合管道替代方案的示例,请参阅:
To learn more about aggregation pipelines, see:要了解有关聚合管道的更多信息,请参阅: