$setWindowFields (aggregation)
On this page本页内容
Definition定义
$setWindowFields
New in version 5.0. 5.0版新增。
Performs operations on a specified span of documents in a collection, known as a window, and returns the results based on the chosen window operator.对集合(称为窗口)中指定的文档范围执行操作,并根据所选窗口运算符返回结果。
For example, you can use the 例如,您可以使用$setWindowFields
stage to output the:$setWindowFields
阶段来输出:
Difference in sales between two documents in a collection.集合中两个文档之间的销售额差异。Sales rankings.销售排名。Cumulative sales totals.累计销售总额。Analysis of complex time series information without exporting the data to an external database.在不将数据导出到外部数据库的情况下分析复杂的时间序列信息。
Syntax语法
The $setWindowFields
stage syntax:$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>: { ... }
}
}
}
The $setWindowFields
stage takes a document with these fields:$setWindowFields
阶段接受一个包含以下字段的文档:
partitionBy | $setWindowFields stage, the group of documents is known as a partition. Default is one partition for the entire collection. $setWindowFields 阶段,文档组称为分区。默认值是整个集合的一个分区。 | |
sortBy | $sort stage. $sort 阶段相同的语法。 | |
output | $setWindowFields stage. $setWindowFields 阶段返回的输出中的文档的字段。$setWindowFields stage are the same as the $addFields and $set stages. $setWindowFields 阶段中嵌入文档点符号的语义与$addFields 和$set 阶段相同。$addFields 示例和嵌入式文档$set 示例。
| |
window | documents 窗口或range 窗口。 | |
documents |
| |
range | sortBy 字段的一系列值来定义下边界和上边界。
| |
unit | range 窗口边界的单位。
range 窗口边界。 |
See also: 另请参阅:
Behavior行为
The $setWindowFields
stage appends new fields to existing documents. You can include one or more $setWindowFields
stages in an aggregation operation.$setWindowFields
阶段将新字段附加到现有文档中。您可以在聚合操作中包括一个或多个$setWindowFields
阶段。
Starting in MongoDB 5.3, you can use the 从MongoDB 5.3开始,您可以将$setWindowFields
stage with transactions and the "snapshot"
read concern.$setWindowFields
阶段用于事务和"snapshot"
读取关注。
Window Operators窗口运算符
These operators can be used with the 这些运算符可以与$setWindowFields
stage:$setWindowFields
阶段一起使用:
Accumulator operators:累计运算符:$addToSet
,$avg
,$bottom
,$bottomN
,$count
,$covariancePop
,$covarianceSamp
,$derivative
,$expMovingAvg
,$firstN
,$integral
,$lastN
,$max
,$maxN
,$median
,$min
,$minN
,$percentile
,$push
,$stdDevSamp
,$stdDevPop
,$sum
,$top
,$topN
.$addToSet
、$avg
、$bottom
、$bottomN
、$count
、$covariancePop
、$covarianceSamp
、$derivative
、$expMovingAvg
、$firstN
,$integral
、$lastN
、$max
、$maxN
、$median
、$min
、$minN
、$percentile
、$push
、$stdDevSamp
、$stdDevPop
、$sum
、$top
、$topN
。
Gap filling operators:缺口填充运算符:$linearFill
and$locf
.$linearFill
和$locf
。
Rank operators:排名运算符:$denseRank
,$documentNumber
, and$rank
.$denseRank
、$documentNumber
和$rank
。
Restrictions限制
Restrictions for the $setWindowFields
stage:$setWindowFields
阶段的限制:
Prior to MongoDB 5.3, the在MongoDB 5.3之前,不能使用$setWindowFields
stage cannot be used:$setWindowFields
阶段:Within transactions.在事务中。With使用"snapshot"
read concern."snapshot"
读取关注。
sortBy is required for:sortBy
用于:Range windows require all sortBy values to be numbers.range
窗口要求所有sortBy
值都是数字。Time range windows require all sortBy values to be dates.时间范围窗口要求所有sortBy
值都是日期。Range and time range windows can only contain one sortBy field and the sort must be ascending.range
和时间范围窗口只能包含一个sortBy
字段,并且排序必须是升序。You cannot specify both a documents window and a range window.不能同时指定documents
窗口和range
窗口。These operators use an implicit window and return an error if you specify a window option:如果指定window
选项,这些运算符将使用隐式窗口并返回错误:For range windows, only numbers in the specified range are included in the window. Missing, undefined, and对于null
values are excluded.range
窗口,窗口中只包含指定范围内的数字。将排除缺失值、未定义值和null
值。For time range windows:对于时间范围窗口:Only date and time types are included in the window.窗口中只包括日期和时间类型。Numeric boundary values must be integers. For example, you can use 2 hours as a boundary but you cannot use 1.5 hours.数值边界值必须是整数。例如,可以使用2小时作为边界,但不能使用1.5小时。
For empty windows or windows with incompatible values (for example, using对于空窗口或具有不兼容值的窗口(例如,在字符串上使用$sum
on strings), the returned value depends on the operator:$sum
),返回的值取决于运算符:
Examples实例
Create a 创建一个包含加利福尼亚州(cakeSales
collection that contains cake sales in the states of California (CA
) and Washington (WA
):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 }
] )
The following examples use the 以下示例使用cakeSales
collection.cakeSales
集合。
Documents Window Examples文档窗口示例
Use Documents Window to Obtain Cumulative Quantity for Each State使用文档窗口获取每个状态的累计数量
This example uses a documents window in 本例使用$setWindowFields
to output the cumulative cake sales quantity
for each state
:$setWindowFields
中的documents
窗口来输出每个state
的累计蛋糕销售quantity
:
db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { orderDate: 1 },
output: {
cumulativeQuantityForState: {
$sum: "$quantity",
window: {
documents: [ "unbounded", "current" ]
}
}
}
}
}
] )
In the example:在示例中:
partitionBy: "$state"
partitions the documents in the collection by按状态对集合中的文档进行分区。state
.There are partitions forCA
andWA
.CA
和WA
有分区。sortBy: { orderDate: 1 }
sorts the documents in each partition by按orderDate
in ascending order (1
), so the earliestorderDate
is first.orderDate
按升序(1
)对每个分区中的文档进行排序,因此最早的orderDate
是第一个。
output:输出:Sets the将cumulativeQuantityForState
field to the cumulativequantity
for eachstate
, which increases by successive additions to the previous value in the partition.cumulativeQuantityForState
字段设置为每个state
的累计quantity
,该数量通过连续添加分区中的上一个值而增加。Calculates the cumulative使用在quantity
using the$sum
operator run in a documents window.documents
窗口中运行的$sum
运算符计算累计quantity
。The window contains documents between an该unbounded
lower limit and thecurrent
document.window
包含介于unbounded
下限和current
文档之间的文档。This means这意味着$sum
returns the cumulativequantity
for the documents between the beginning of the partition and the current document.$sum
返回分区开始和当前文档之间文档的累计quantity
。
In this example output, the cumulative 在此示例输出中,quantity
for CA
and WA
is shown in the cumulativeQuantityForState
field:CA
和WA
的累计quantity
显示在cumulativeQuantityForState
字段中:
{ "_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 }
Use Documents Window to Obtain Cumulative Quantity for Each Year使用文档窗口获取每年的累计数量
This example uses a documents window in 本例使用$setWindowFields
to output the cumulative cake sales quantity
for each $year
in orderDate
:$setWindowFields
中的documents
窗口输出orderDate
中每$year
的累计蛋糕销售quantity
:
db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: { $year: "$orderDate" },
sortBy: { orderDate: 1 },
output: {
cumulativeQuantityForYear: {
$sum: "$quantity",
window: {
documents: [ "unbounded", "current" ]
}
}
}
}
}
] )
In the example:在示例中:
partitionBy: { $year: "$orderDate" }
partitions the documents in the collection by按$year
inorderDate
. There are are partitions for2019
,2020
, and2021
.orderDate
中的$year
对集合中的文档进行分区。有2019
年、2020
年和2021
年的分区。sortBy: { orderDate: 1 }
sorts the documents in each partition by按orderDate
in ascending order (1
), so the earliestorderDate
is first.orderDate
按升序(1
)对每个分区中的文档进行排序,因此最早的orderDate
是第一个。output:输出:Sets the将cumulativeQuantityForYear
field to the cumulativequantity
for each year, which increases by successive additions to the previous value in the partition.cumulativeQuantityForYear
字段设置为每年的累计数量,该数量通过连续添加分区中的上一个值而增加。Calculates the cumulative使用在quantity
using the$sum
operator run in a documents window.documents
窗口中运行的$sum
运算符计算累计quantity
。The window contains documents between an该窗口包含介于unbounded
lower limit and thecurrent
document.unbounded
下限和current
文档之间的文档。This means这意味着$sum
returns the cumulativequantity
for the documents between the beginning of the partition and the current document.$sum
返回分区开始和当前文档之间文档的累计quantity
。
In this example output, the cumulative 在此示例输出中,每年的累计quantity
for each year is shown in the cumulativeQuantityForYear
field: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 }
Use Documents Window to Obtain Moving Average Quantity for Each Year使用文档窗口获取每年的移动平均数量
This example uses a documents window in 本例使用$setWindowFields
to output the moving average for the cake sales quantity
:$setWindowFields
中的documents
窗口来输出蛋糕销售quantity
的移动平均值:
db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: { $year: "$orderDate" },
sortBy: { orderDate: 1 },
output: {
averageQuantity: {
$avg: "$quantity",
window: {
documents: [ -1, 0 ]
}
}
}
}
}
] )
In the example:在示例中:
partitionBy: "$orderDate"
partitions the documents in the collection by按$year
inorderDate
. There are are partitions for2019
,2020
, and2021
.orderDate
中的$year
对集合中的文档进行分区。有2019
年、2020
年和2021
年的分区。sortBy: { orderDate: 1 }
sorts the documents in each partition by按orderDate
in ascending order (1
), so the earliestorderDate
is first.orderDate
按升序(1
)对每个分区中的文档进行排序,因此最早的orderDate
是第一个。output:输出:Sets the将averageQuantity
field to the moving averagequantity
for each year.averageQuantity
字段设置为每年的移动平均quantity
。Calculates the moving average使用在quantity
using the$avg
operator run in a documents window.documents
窗口中运行的$avg
运算符计算移动平均quantity
。The window contains documents between该-1
and0
. This means$avg
returns the moving averagequantity
between the document before the current document (-1
) and the current document (0
) in the partition.window
包含介于-1
和0
之间的文档。这意味着$avg
返回分区中当前文档(-1
)之前的文档和当前文档(0
)之间的移动平均数量。
In this example output, the moving average 在本示例输出中,移动平均quantity
is shown in the averageQuantity
field:quantity
显示在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 }
Use Documents Window to Obtain Cumulative and Maximum Quantity for Each Year使用文档窗口获取每年的累计数量和最大数量
This example uses a documents window in 本例使用$setWindowFields
to output the cumulative and maximum cake sales quantity
values for each $year
in orderDate
:$setWindowFields
中的documents
窗口来输出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" ]
}
}
}
}
}
] )
In the example:在示例中:
partitionBy: "$orderDate"
partitions the documents in the collection by按$year
inorderDate
. There are are partitions for2019
,2020
, and2021
.orderDate
中的$year
对集合中的文档进行分区。有2019
年、2020
年和2021
年的分区。sortBy: { orderDate: 1 }
sorts the documents in each partition by按orderDate
in ascending order (1
), so the earliestorderDate
is first.orderDate
按升序(1
)对每个分区中的文档进行排序,因此最早的orderDate
是第一个。output:输出:Sets the将cumulativeQuantityForYear
field to the cumulativequantity
for each year.cumulativeQuantityForYear
字段设置为每年的累计quantity
。Calculates the cumulative使用在quantity
using the$sum
operator run in a documents window.documents
窗口中运行的$sum
运算符计算累计quantity
。The window contains documents between an该unbounded
lower limit and thecurrent
document. This means$sum
returns the cumulative quantity for the documents between the beginning of the partition and the current document.window
包含介于unbounded
下限和current
文档之间的文档。这意味着$sum
返回分区开始和当前文档之间文档的累计数量。Sets the将maximumQuantityForYear
field to the maximumquantity
for each year.maximumQuantityForYear
字段设置为每年的最大数量。Calculates the maximum使用在quantity
of all the documents using the$max
operator run in a documents window.documents
窗口中运行的$max
运算符计算所有文档的最大quantity
。The window contains documents between an该unbounded
lower andupper
limit.window
包含介于unbounded
下限和上限之间的文档。This means这意味着$max
returns the maximum quantity for the documents in the partition.$max
将返回分区中文档的最大数量。
In this example output, the cumulative 在此示例输出中,累计quantity
is shown in the cumulativeQuantityForYear
field and the maximum quantity
is shown in the maximumQuantityForYear
field:quantity
显示在cumulativeQuantityForYear
字段中,最大quantity
显示在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 }
Range Window Example范围窗口示例
This example uses a range window in 本例使用$setWindowFields
to return the sum of the quantity
values of cakes sold for orders within plus or minus 10 dollars of the current document's price
value:$setWindowFields
中的range
窗口,返回当前文档price
值的正负10美元范围内订单销售的蛋糕quantity
值的总和:
db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { price: 1 },
output: {
quantityFromSimilarOrders: {
$sum: "$quantity",
window: {
range: [ -10, 10 ]
}
}
}
}
}
] )
In the example:在示例中:
partitionBy: "$state"
partitions the documents in the collection by按state
. There are partitions forCA
andWA
.state
对集合中的文档进行分区。CA
和WA
有分区。sortBy: { price: 1 }
sorts the documents in each partition by按price
in ascending order (1
), so the lowestprice
is first.price
升序(1
)对每个分区中的文档进行排序,因此最低price
是第一位的。output
sets the将quantityFromSimilarOrders
field to the sum of thequantity
values from the documents in a range window.quantityFromSimilarOrders
字段设置为range
窗口中文档的quantity
值之和。The window contains documents between a lower limit of该窗口包含介于-10
and an upper limit of10
. The range is inclusive.-10
的下限和10
的上限之间的文档。范围包括在内。$sum
returns the sum of返回当前文档quantity
values contained in a range of plus or minus 10 dollars of the current document'sprice
value.price
值的正负10美元范围内包含的quantity
值的总和。
In this example output, the sum of the 在此示例输出中,窗口中文档的quantity
values for documents in the window is shown in the quantityFromSimilarOrders
field:quantity
值之和显示在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 }
Time Range Window Examples时间范围窗口示例
Use a Time Range Window with a Positive Upper Bound使用具有正上限的时间范围窗口
The following example uses a window with a positive upper bound time range unit in 以下示例在$setWindowFields
. $setWindowFields
中使用一个具有正上限时间范围unit
的window
。The pipeline outputs an array of 管道为每个与指定时间范围匹配的orderDate
values for each state
that match the specified time range.state
输出一个orderDate
值数组。
db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { orderDate: 1 },
output: {
recentOrders: {
$push: "$orderDate",
window: {
range: [ "unbounded", 10 ],
unit: "month"
}
}
}
}
}
] )
In the example:在示例中:
partitionBy: "$state"
partitions the documents in the collection by按state
. There are partitions forCA
andWA
.state
对集合中的文档进行分区。CA
和WA
有分区。sortBy: { orderDate: 1 }
sorts the documents in each partition by按orderDate
in ascending order (1
), so the earliestorderDate
is first.orderDate
按升序(1
)对每个分区中的文档进行排序,因此最早的orderDate是第一个。output:输出:Sets the将每个orderDateArrayForState
array field toorderDate
values for the documents in eachstate
.state
下文档的orderDateArrayForState
数组字段设置为orderDate
值。The array elements are expanded with additions to the previous elements in the array.数组元素是通过添加数组中先前的元素来展开的。Uses使用$push
to return an array oforderDate
values from the documents in a range window.$push
返回range
窗口中文档的orderDate
值数组。
The window contains documents between an该unbounded
lower limit and an upper limit set to10
(10 months after the current document'sorderDate
value) using a time range unit.window
包含unbounded
下限和使用时间范围单位设置为10
(当前文档的orderDate
值后10个月)的上限之间的文档。$push
returns the array oforderDate
values for the documents between the beginning of the partition and the documents withorderDate
values inclusively in a range of the current document'sorderDate
value plus10
months.$push
返回文档的orderDate
值数组,该数组位于分区的开头和orderDate
值包含在当前文档的orderDate
值加上10
个月的范围内的文档之间。
In this example output, the array of 在此示例输出中,orderDate
values for CA
and WA
is shown in the recentOrders
field:CA
和WA
的orderDate
值数组显示在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") ] }
Use a Time Range Window with a Negative Upper Bound使用具有负上限的时间范围窗口
The following example uses a window with a negative upper bound time range unit in 以下示例在$setWindowFields
. $setWindowFields
中使用一个具有负上限时间范围unit
的window
。The pipeline outputs an array of 管道为每个与指定时间范围匹配的orderDate
values for each state
that match the specified time range.state
输出一个orderDate
值数组。
db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { orderDate: 1 },
output: {
recentOrders: {
$push: "$orderDate",
window: {
range: [ "unbounded", -10 ],
unit: "month"
}
}
}
}
}
] )
In the example:在示例中:
partitionBy: "$state"
partitions the documents in the collection by按state
. There are partitions forCA
andWA
.state
对集合中的文档进行分区。CA
和WA
有分区。sortBy: { orderDate: 1 }
sorts the documents in each partition by按orderDate
in ascending order (1
), so the earliestorderDate
is first.orderDate
按升序(1
)对每个分区中的文档进行排序,因此最早的orderDate
是第一个。output:输出:Sets the将每个orderDateArrayForState
array field toorderDate
values for the documents in eachstate
.state
下文档的orderDateArrayForState
数组字段设置为orderDate
值。The array elements are expanded with additions to the previous elements in the array.数组元素是通过添加数组中先前的元素来展开的。Uses使用$push
to return an array oforderDate
values from the documents in a range window.$push
返回range
窗口中文档的orderDate
值数组。
The window contains documents between an该unbounded
lower limit and an upper limit set to-10
(10 months before the current document'sorderDate
value) using a time range unit.window
包含unbounded
下限和使用时间范围单位设置为-10
(当前文档的orderDate
值之前10个月)的上限之间的文档。$push
returns the array of返回位于分区开头和orderDate
values for the documents between the beginning of the partition and the documents withorderDate
values inclusively in a range of the current document'sorderDate
value minus10
months.orderDate
值包含在当前文档的orderDate
值减去10
个月的范围内的文档的orderDate
值的数组。
In this example output, the array of 在此示例输出中,orderDate
values for CA
and WA
is shown in the recentOrders
field:CA
和WA
的orderDate
值数组显示在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") ] }
See also: 另请参阅:
For an additional example about IOT Power Consumption, see the Practical MongoDB Aggregations有关IOT功耗的其他示例,请参阅实用MongoDB聚合电子书。 e-book.