$push(聚合)
定义
$push
可在以下阶段使用:
$setWindowFields
(从 MongoDB 5.0 开始提供)
语法
$push
事务语法:
{ $push: <expression> }
有关表达式的更多信息,请参阅表达式运算符。
行为
在 $group
阶段使用 $push
时,进入管道的文档顺序决定了输出数组中的文档顺序。为了保证采用所定义的顺序,$group
管道阶段应遵循 $sort
阶段。
示例
在$group
阶段使用
请考虑包含以下文档的 sales
集合:
{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-01-01T08:00:00Z") } { "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-02-03T09:00:00Z") } { "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 5, "date" : ISODate("2014-02-03T09:05:00Z") } { "_id" : 4, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-02-15T08:00:00Z") } { "_id" : 5, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-02-15T09:05:00Z") } { "_id" : 6, "item" : "xyz", "price" : 5, "quantity" : 5, "date" : ISODate("2014-02-15T12:05:10Z") } { "_id" : 7, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-02-15T14:12:12Z") }
通过按照 date
字段的日期和年份对文档进行分组,以下操作使用 $push
累加器计算每组的商品和销售数量清单:
db.sales.aggregate( [ { $sort: { date: 1, item: 1 } }, { $group: { _id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } }, itemsSold: { $push: { item: "$item", quantity: "$quantity" } } } } ] )
操作返回以下结果:
{ "_id" : { "day" : 46, "year" : 2014 }, "itemsSold" : [ { "item" : "abc", "quantity" : 10 }, { "item" : "xyz", "quantity" : 10 }, { "item" : "xyz", "quantity" : 5 }, { "item" : "xyz", "quantity" : 10 } ] } { "_id" : { "day" : 34, "year" : 2014 }, "itemsSold" : [ { "item" : "jkl", "quantity" : 1 }, { "item" : "xyz", "quantity" : 5 } ] } { "_id" : { "day" : 1, "year" : 2014 }, "itemsSold" : [ { "item" : "abc", "quantity" : 2 } ] }
在$setWindowFields
阶段使用
版本 5.0 中的新增功能。
创建cakeSales
集合,其中包含加利福尼亚州 ( CA
) 和华盛顿州 ( WA
) 的蛋糕销售情况:
db.cakeSales.insertMany( [ { _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"), state: "CA", price: 13, quantity: 120 }, { _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"), state: "WA", price: 14, quantity: 140 }, { _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"), state: "CA", price: 12, quantity: 145 }, { _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"), state: "WA", price: 13, quantity: 104 }, { _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"), state: "CA", price: 41, quantity: 162 }, { _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"), state: "WA", price: 43, quantity: 134 } ] )
此示例在 $setWindowFields
阶段中使用 $push
为每个 state
输出蛋糕销售 quantity
值的数组:
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { orderDate: 1 }, output: { quantitiesForState: { $push: "$quantity", window: { documents: [ "unbounded", "current" ] } } } } } ] )
在示例中:
partitionBy: "$state"
按state
对集合中的文档分区。CA
和WA
都有分区。sortBy: { orderDate: 1 }
按orderDate
以升序 (1
) 对每个分区中的文档进行排序,因此最早的orderDate
位于最前面。
output
设置使用文档窗口中文档$push
的quantity
值数组。该窗口包含介于
unbounded
下限与输出中current
文档之间的文档。这意味着$push
会将分区起点和当前文档之间的quantity
值追加到quantitiesForState
数组中。
在此输出中,CA
和 WA
的 quantity
值数组显示在 quantitiesForState
数组中:
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"), "state" : "CA", "price" : 41, "quantity" : 162, "quantitiesForState" : [ 162 ] } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"), "state" : "CA", "price" : 13, "quantity" : 120, "quantitiesForState" : [ 162, 120 ] } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"), "state" : "CA", "price" : 12, "quantity" : 145, "quantitiesForState" : [ 162, 120, 145 ] } { "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"), "state" : "WA", "price" : 43, "quantity" : 134, "quantitiesForState" : [ 134 ] } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"), "state" : "WA", "price" : 13, "quantity" : 104, "quantitiesForState" : [ 134, 104 ] } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"), "state" : "WA", "price" : 14, "quantity" : 140, "quantitiesForState" : [ 134, 104, 140 ] }