$setWindowFields(聚合)
定义
新增在版本5.0.
对集合中指定范围的文档(称为窗口)执行操作,并基于所选窗口操作符.
例如,您可以使用$setWindowFields
阶段来输出
集合中两个文档之间的销售差异。
销售排名。
累计销售总额。
分析复杂的时间序列信息,而无需将数据导出到外部数据库。
语法
阶段$setWindowFields
的语法
{ $setWindowFields: { partitionBy: <expression>, sortBy: { <sort field 1>: <sort order>, <sort field 2>: <sort order>, ..., <sort field n>: <sort order> }, output: { <output field 1>: { <window operator>: <window operator parameters>, window: { documents: [ <lower boundary>, <upper boundary> ], range: [ <lower boundary>, <upper boundary> ], unit: <time unit> } }, <output field 2>: { ... }, ... <output field n>: { ... } } } }
阶段$setWindowFields
接受包含以下字段的文档
字段 | 必需性 | 描述 |
---|---|---|
可选 | 指定一个表达式来分组文档。在 | |
某些操作符必需(见限制) | 指定在分区中对文档进行排序的字段。使用与 | |
必需 | 指定要添加到 字段可以包含点来指定嵌套文档字段和数组字段。在
| |
可选 | ||
可选 | 一个窗口,其上下边界相对于从集合中读取的当前文档位置进行指定。 窗口边界使用一个包含下限和上限字符串或整数的两个元素数组进行指定。使用
请参阅文档窗口示例。 | |
可选 | ||
可选 |
行为
$setWindowFields
阶段会将新字段追加到现有文档中。您可以在聚合操作中包含一个或多个 $setWindowFields
阶段。
从 MongoDB 5.3 版本开始,您可以使用 $setWindowFields
阶段与 事务 以及 "snapshot"
读取关注点一起使用。
$setWindowFields
阶段不保证返回文档的顺序。
窗口操作符
这些操作符可以与 $setWindowFields
阶段一起使用
累加器操作符:
$addToSet
,$avg
,$bottom
,$bottomN
,$count
,$covariancePop
,$covarianceSamp
,$derivative
,$expMovingAvg
,$firstN
,$integral
,$lastN
,$max
,$maxN
,$median
,$min
,$minN
,$percentile
,$push
,$stdDevSamp
,$stdDevPop
,$sum
,$top
,$topN
。
插值操作符:
$linearFill
和$locf
。
排名操作符:
$denseRank
,$documentNumber
,和$rank
。
限制
针对 $setWindowFields
阶段的限制
在 MongoDB 5.3 之前,无法使用
$setWindowFields
阶段在 事务 中。
带有
"snapshot"
读取关注点的。
sortBy 对 Rank 和 order 窗口操作符是必需的。
有界窗口(无论是文档窗口还是范围窗口)。
$linearFill
操作符。
范围窗口需要所有 sortBy 值都是数字。
时间范围窗口需要所有 sortBy 值都是日期。
范围和时间范围窗口只能包含一个 sortBy 字段,且排序必须是升序的。
您不能同时指定文档窗口和范围窗口。
这些操作符使用隐式窗口,如果指定了窗口选项,则返回错误。
对于范围窗口,只有指定范围内的数字包含在窗口中。排除缺失、未定义和
null
值。对于时间范围窗口
窗口中仅包含日期和时间类型。
数值边界值必须是整数。例如,您可以使用2小时作为边界,但不能使用1.5小时。
对于空窗口或不兼容值的窗口(例如,在字符串上使用
$sum
),返回值取决于运算符
示例
创建一个包含加利福尼亚州(CA
)和华盛顿州(WA
)蛋糕销售的cakeSales
集合
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 } ] )
以下示例使用cakeSales
集合。
文档窗口示例
使用文档窗口获取每个州的累计数量
此示例使用 文档 窗口在 $setWindowFields
中输出每个 state
的累计蛋糕销售 quantity
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { orderDate: 1 }, output: { cumulativeQuantityForState: { $sum: "$quantity", window: { documents: [ "unbounded", "current" ] } } } } } ] )
在示例中
partitionBy: "$state"
根据state
对集合中的文档进行 分区。有CA
和WA
的分区。sortBy: { orderDate: 1 }
对每个分区中的文档按orderDate
进行 排序,升序(1
),因此最早的orderDate
是第一个。
output
:
在此示例输出中,显示在 cumulativeQuantityForState
字段中的 CA
和 WA
的累计 quantity
。
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"), "state" : "CA", "price" : 41, "quantity" : 162, "cumulativeQuantityForState" : 162 } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"), "state" : "CA", "price" : 13, "quantity" : 120, "cumulativeQuantityForState" : 282 } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"), "state" : "CA", "price" : 12, "quantity" : 145, "cumulativeQuantityForState" : 427 } { "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"), "state" : "WA", "price" : 43, "quantity" : 134, "cumulativeQuantityForState" : 134 } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"), "state" : "WA", "price" : 13, "quantity" : 104, "cumulativeQuantityForState" : 238 } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"), "state" : "WA", "price" : 14, "quantity" : 140, "cumulativeQuantityForState" : 378 }
使用文档窗口获取每年累积数量
此示例使用 documents 窗口在 $setWindowFields
中输出 orderDate
中每年的累积蛋糕销量 quantity
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: { $year: "$orderDate" }, sortBy: { orderDate: 1 }, output: { cumulativeQuantityForYear: { $sum: "$quantity", window: { documents: [ "unbounded", "current" ] } } } } } ] )
在示例中
partitionBy: { $year: "$orderDate" }
根据orderDate
中的$year
将集合中的文档进行$year
分区。存在2019
、2020
和2021
分区。sortBy: { orderDate: 1 }
对每个分区中的文档按orderDate
进行 排序,升序(1
),因此最早的orderDate
是第一个。output
:
在此示例输出中,每年的累积 quantity
显示在 cumulativeQuantityForYear
字段中
{ "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"), "state" : "WA", "price" : 43, "quantity" : 134, "cumulativeQuantityForYear" : 134 } { "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"), "state" : "CA", "price" : 41, "quantity" : 162, "cumulativeQuantityForYear" : 296 } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"), "state" : "WA", "price" : 13, "quantity" : 104, "cumulativeQuantityForYear" : 104 } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"), "state" : "CA", "price" : 13, "quantity" : 120, "cumulativeQuantityForYear" : 224 } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"), "state" : "CA", "price" : 12, "quantity" : 145, "cumulativeQuantityForYear" : 145 } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"), "state" : "WA", "price" : 14, "quantity" : 140, "cumulativeQuantityForYear" : 285 }
使用文档窗口获取每年移动平均数量
此示例使用 文档 窗口在 $setWindowFields
中输出蛋糕销售 数量
的移动平均值。
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: { $year: "$orderDate" }, sortBy: { orderDate: 1 }, output: { averageQuantity: { $avg: "$quantity", window: { documents: [ -1, 0 ] } } } } } ] )
在示例中
partitionBy: "$orderDate"
根据orderDate
中的$year
将集合中的文档进行分区。存在2019
、2020
和2021
的分区。sortBy: { orderDate: 1 }
对每个分区中的文档按orderDate
进行 排序,升序(1
),因此最早的orderDate
是第一个。output
:
在此示例输出中,移动平均 数量
显示在 averageQuantity
字段中。
{ "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"), "state" : "WA", "price" : 43, "quantity" : 134, "averageQuantity" : 134 } { "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"), "state" : "CA", "price" : 41, "quantity" : 162, "averageQuantity" : 148 } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"), "state" : "WA", "price" : 13, "quantity" : 104, "averageQuantity" : 104 } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"), "state" : "CA", "price" : 13, "quantity" : 120, "averageQuantity" : 112 } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"), "state" : "CA", "price" : 12, "quantity" : 145, "averageQuantity" : 145 } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"), "state" : "WA", "price" : 14, "quantity" : 140, "averageQuantity" : 142.5 }
使用文档窗口获取每年累积和最大数量
此示例使用 文档 窗口在 $setWindowFields
中输出每个 orderDate
中 $year
的累积和最大蛋糕销售 数量
值。
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: { $year: "$orderDate" }, sortBy: { orderDate: 1 }, output: { cumulativeQuantityForYear: { $sum: "$quantity", window: { documents: [ "unbounded", "current" ] } }, maximumQuantityForYear: { $max: "$quantity", window: { documents: [ "unbounded", "unbounded" ] } } } } } ] )
在示例中
partitionBy: "$orderDate"
根据orderDate
中的$year
将集合中的文档进行分区。存在2019
、2020
和2021
的分区。sortBy: { orderDate: 1 }
对每个分区中的文档按orderDate
进行 排序,升序(1
),因此最早的orderDate
是第一个。output
:
在此示例输出中,累积 数量
显示在 cumulativeQuantityForYear
字段中,最大 数量
显示在 maximumQuantityForYear
字段中
{ "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"), "state" : "WA", "price" : 43, "quantity" : 134, "cumulativeQuantityForYear" : 134, "maximumQuantityForYear" : 162 } { "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"), "state" : "CA", "price" : 41, "quantity" : 162, "cumulativeQuantityForYear" : 296, "maximumQuantityForYear" : 162 } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"), "state" : "WA", "price" : 13, "quantity" : 104, "cumulativeQuantityForYear" : 104, "maximumQuantityForYear" : 120 } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"), "state" : "CA", "price" : 13, "quantity" : 120, "cumulativeQuantityForYear" : 224, "maximumQuantityForYear" : 120 } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"), "state" : "CA", "price" : 12, "quantity" : 145, "cumulativeQuantityForYear" : 145, "maximumQuantityForYear" : 145 } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"), "state" : "WA", "price" : 14, "quantity" : 140, "cumulativeQuantityForYear" : 285, "maximumQuantityForYear" : 145 }
范围窗口示例
此示例使用 范围 窗口在 $setWindowFields
中返回当前文档 价格
值加减 10 美元的订单所售蛋糕的 数量
值总和。
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { price: 1 }, output: { quantityFromSimilarOrders: { $sum: "$quantity", window: { range: [ -10, 10 ] } } } } } ] )
在示例中
partitionBy: "$state"
根据state
对集合中的文档进行 分区。有CA
和WA
的分区。sortBy: { price: 1 }
按价格
递增顺序(1
)对每个分区的文档进行排序,因此最低价格
最先。output
将quantityFromSimilarOrders
字段设置为从 范围 窗口中的文档中数量
值的总和。
在此示例输出中,窗口中文档的 数量
值总和显示在 quantityFromSimilarOrders
字段中
{ "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"), "state" : "CA", "price" : 12, "quantity" : 145, "quantityFromSimilarOrders" : 265 } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"), "state" : "CA", "price" : 13, "quantity" : 120, "quantityFromSimilarOrders" : 265 } { "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"), "state" : "CA", "price" : 41, "quantity" : 162, "quantityFromSimilarOrders" : 162 } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"), "state" : "WA", "price" : 13, "quantity" : 104, "quantityFromSimilarOrders" : 244 } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"), "state" : "WA", "price" : 14, "quantity" : 140, "quantityFromSimilarOrders" : 244 } { "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"), "state" : "WA", "price" : 43, "quantity" : 134, "quantityFromSimilarOrders" : 134 }
时间范围窗口示例
使用具有正上界的时间范围窗口
以下示例使用了一个具有正上界时间范围单位的时间范围窗口窗口,在单位中$setWindowFields
。管道输出每个匹配指定时间范围的state
的orderDate
值数组。
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { orderDate: 1 }, output: { recentOrders: { $push: "$orderDate", window: { range: [ "unbounded", 10 ], unit: "month" } } } } } ] )
在示例中
partitionBy: "$state"
根据state
对集合中的文档进行 分区。有CA
和WA
的分区。sortBy: { orderDate: 1 }
对每个分区中的文档按orderDate
进行 排序,升序(1
),因此最早的orderDate
是第一个。output
:
在本例输出中,显示的是orderDate
数组,该数组对应于CA
和WA
,并在recentOrders
字段中展示。
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"), "state" : "CA", "price" : 41, "quantity" : 162, "recentOrders" : [ ISODate("2019-05-18T16:09:01Z") ] } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"), "state" : "CA", "price" : 13, "quantity" : 120, "recentOrders" : [ ISODate("2019-05-18T16:09:01Z"), ISODate("2020-05-18T14:10:30Z"), ISODate("2021-01-11T06:31:15Z") ] } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"), "state" : "CA", "price" : 12, "quantity" : 145, "recentOrders" : [ ISODate("2019-05-18T16:09:01Z"), ISODate("2020-05-18T14:10:30Z"), ISODate("2021-01-11T06:31:15Z") ] } { "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"), "state" : "WA", "price" : 43, "quantity" : 134, "recentOrders" : [ ISODate("2019-01-08T06:12:03Z") ] } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"), "state" : "WA", "price" : 13, "quantity" : 104, "recentOrders" : [ ISODate("2019-01-08T06:12:03Z"), ISODate("2020-02-08T13:13:23Z") ] } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"), "state" : "WA", "price" : 14, "quantity" : 140, "recentOrders" : [ ISODate("2019-01-08T06:12:03Z"), ISODate("2020-02-08T13:13:23Z"), ISODate("2021-03-20T11:30:05Z") ] }
使用带有负上限的时间范围窗口
以下示例使用了一个带有负上限时间范围窗口和一个时间范围单位的$setWindowFields
。该管道输出一个数组,包含每个匹配指定时间范围的state
的orderDate
值。
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { orderDate: 1 }, output: { recentOrders: { $push: "$orderDate", window: { range: [ "unbounded", -10 ], unit: "month" } } } } } ] )
在示例中
partitionBy: "$state"
根据state
对集合中的文档进行 分区。有CA
和WA
的分区。sortBy: { orderDate: 1 }
对每个分区中的文档按orderDate
进行 排序,升序(1
),因此最早的orderDate
是第一个。output
:
该窗口包含从
unbounded
下限到设置为-10
(当前文档的orderDate
值10个月前)的上限之间的文档,使用时间范围单位。$push
返回从分区开始到当前文档的orderDate
值减去10个月的文档的orderDate
值的数组。
在本例输出中,显示的是orderDate
数组,该数组对应于CA
和WA
,并在recentOrders
字段中展示。
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"), "state" : "CA", "price" : 41, "quantity" : 162, "recentOrders" : [ ] } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"), "state" : "CA", "price" : 13, "quantity" : 120, "recentOrders" : [ ISODate("2019-05-18T16:09:01Z") ] } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"), "state" : "CA", "price" : 12, "quantity" : 145, "recentOrders" : [ ISODate("2019-05-18T16:09:01Z") ] } { "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"), "state" : "WA", "price" : 43, "quantity" : 134, "recentOrders" : [ ] } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"), "state" : "WA", "price" : 13, "quantity" : 104, "recentOrders" : [ ISODate("2019-01-08T06:12:03Z") ] } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"), "state" : "WA", "price" : 14, "quantity" : 140, "recentOrders" : [ ISODate("2019-01-08T06:12:03Z"), ISODate("2020-02-08T13:13:23Z") ] }