On this page本页内容
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
阶段输出:
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 |
| |
sortBy |
| |
output |
| |
window |
| |
documents |
| |
range |
| |
unit |
|
The $setWindowFields
stage appends new fields to existing documents. $setWindowFields
阶段将新字段附加到现有文档中。You can include one or more 可以在聚合操作中包含一个或多个$setWindowFields
stages in an aggregation operation.$setWindowFields
阶段。
Starting in MongoDB 5.3, you can use the 从MongoDB 5.3开始,您可以使用$setWindowFields
stage with transactions and the "snapshot"
read concern.$setWindowFields
阶段处理事务和"snapshot"
读取问题。
These operators can be used with the 这些运算符可用于$setWindowFields
stage:$setWindowFields
阶段:
$addToSet
$avg
$bottom
$bottomN
$count
$covariancePop
$covarianceSamp
$derivative
$expMovingAvg
$firstN
$integral
$lastN
$max
$maxN
$min
$minN
$push
$stdDevSamp
$stdDevPop
$sum
$top
$topN
$linearFill
$locf
$denseRank
, $documentNumber
$rank
Restrictions for the $setWindowFields
stage:$setWindowFields
阶段的限制:
Starting in MongoDB 5.1 (and 5.0.4), the 从MongoDB 5.1(和5.0.4)开始,$setWindowFields
stage cannot be used:$setWindowFields
阶段不能在以下情况中使用:
"snapshot"
read concern."snapshot"
读取关注点配合。sortBy
is required for:对以下情况必不可少:
sortBy
值都是数字。sortBy
值都是日期。sortBy
字段,排序必须是升序。These operators use an implicit window and return an error if you specify a window option:如果指定window
选项,这些运算符将使用隐式窗口并返回错误:
null
values are excluded.null
值。For time range windows:对于时间范围窗口:
For empty windows or windows with incompatible values (for example, using 对于空窗口或具有不兼容值的窗口(例如,在字符串上使用$sum
on strings), the returned value depends on the operator:$sum
),返回值取决于运算符:
Create a 创建cakeSales
collection that contains cake sales in the states of California (CA
) and Washington (WA
):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 } ] )
The following examples use the 以下示例使用cakeSales
collection.cakeSales
集合。
This example uses a documents window in 本例使用$setWindowFields
to output the cumulative cake sales quantity
for each state
:$setWindowFields
中的文档窗口输出每个州的累积蛋糕销售数量:
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { orderDate: 1 }, output: { cumulativeQuantityForState: { $sum: "$quantity", window: { documents: [ "unbounded", "current" ] } } } } } ] )
In the example:在示例中:
partitionBy: "$state"
state
. CA
and WA
.CA
和WA
有分区。sortBy: { orderDate: 1 }
orderDate
in ascending order (1
), so the earliest orderDate
is first.orderDate
按升序(1
)对每个分区中的文档进行排序,因此最早的orderDate
是第一个。output
:
cumulativeQuantityForState
field to the cumulative quantity
for each state
, which increases by successive additions to the previous value in the partition.cumulativeQuantityForState
字段设置为每个州的累积数量,该数量通过连续添加到分区中的前一个值来增加。Calculates the cumulative 使用文档窗口中运行的quantity
using the $sum
operator run in a documents window.$sum
运算符计算累计数量。
The window contains documents between an 该窗口包含的文档介于unbounded
lower limit and the current
document. unbounded
下限和current
文档之间。This means 这意味着$sum
returns the cumulative quantity
for the documents between the beginning of the partition and the current document.$sum
返回分区开始和当前文档之间的文档累积数量。
In this example output, the cumulative 在此示例输出中,quantity
for CA
and WA
is shown in the cumulativeQuantityForState
field:CA
和WA
的累积数量显示在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 }
This example uses a documents window in 本例使用$setWindowFields
to output the cumulative cake sales quantity
for each $year
in orderDate
:$setWindowFields
中的文档窗口输出orderDate
中每个$year
的累积蛋糕销售数量:
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: { $year: "$orderDate" }, sortBy: { orderDate: 1 }, output: { cumulativeQuantityForYear: { $sum: "$quantity", window: { documents: [ "unbounded", "current" ] } } } } } ] )
In the example:在示例中:
partitionBy: "$state"
$year
in orderDate
. orderDate
中的$year
对集合中的文档进行分区。2019
, 2020
, and 2021
.2019
、2020
、2021
三个分区。sortBy: { orderDate: 1 }
orderDate
in ascending order (1
), so the earliest orderDate
is first.orderDate
按升序(1
)对每个分区中的文档进行排序,因此最早的orderDate
是第一个。output
:
cumulativeQuantityForYear
field to the cumulative quantity
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.$sum
运算符计算累计数量。
The window contains documents between an 该窗口包含的文档介于unbounded
lower limit and the current
document. unbounded
下限和current
文档之间。This means 这意味着$sum
returns the cumulative quantity
for the documents between the beginning of the partition and the current document.$sum
返回分区开始和当前文档之间的文档累积数量。
In this example output, the cumulative 在本例输出中,每年的累计数量显示在quantity
for each year is shown in the cumulativeQuantityForYear
field: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 }
This example uses a documents window in 本例使用$setWindowFields
to output the moving average for the cake sales quantity
:$setWindowFields
中的文档窗口输出蛋糕销售数量的移动平均值:
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: { $year: "$orderDate" }, sortBy: { orderDate: 1 }, output: { averageQuantity: { $avg: "$quantity", window: { documents: [ -1, 0 ] } } } } } ] )
In the example:在示例中:
partitionBy: "$orderDate"
$year
in orderDate
. orderDate
中的$year
对集合中的文档进行分区。2019
, 2020
, and 2021
.2019
、2020
、2021
三个分区。sortBy: { orderDate: 1 }
orderDate
in ascending order (1
), so the earliest orderDate
is first.orderDate
按升序(1
)对每个分区中的文档进行排序,因此最早的orderDate
是第一个。output
:
averageQuantity
field to the moving average quantity
for each year.averageQuantity
字段设置为每年的移动平均数量。Calculates the moving average 使用在文档窗口中运行的quantity
using the $avg
operator run in a documents window.$avg
运算符计算移动平均数量。
The window contains documents between 该窗口包含介于-1
and 0
. -1
和0
之间的文档。This means 这意味着$avg
returns the moving average quantity
between the document before the current document (-1
) and the current document (0
) in the partition.$avg
返回分区中当前文档(-1
)之前的文档和当前文档(0
)之间的移动平均数量。
In this example output, the moving average 在此示例输出中,移动平均数量显示在quantity
is shown in the averageQuantity
field: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 }
This example uses a documents window in 本例使用$setWindowFields
to output the cumulative and maximum cake sales quantity
values for each $year
in orderDate
:$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" ] } } } } } ] )
In the example:在示例中:
partitionBy: "$orderDate"
$year
in orderDate
. orderDate
中的$year
对集合中的文档进行分区。2019
, 2020
, and 2021
.2019
、2020
、2021
三个分区。sortBy: { orderDate: 1 }
orderDate
in ascending order (1
), so the earliest orderDate
is first.orderDate
按升序(1
)对每个分区中的文档进行排序,因此最早的orderDate
是第一个。output
:
cumulativeQuantityForYear
field to the cumulative quantity
for each year.cumulativeQuantityForYear
字段设置为每年的累积数量。Calculates the cumulative 使用文档窗口中运行的quantity
using the $sum
operator run in a documents window.$sum
运算符计算累计数量。
The window contains documents between an 该窗口包含的文档介于unbounded
lower limit and the current
document. unbounded
下限和current
文档之间。This means 这意味着$sum
returns the cumulative quantity for the documents between the beginning of the partition and the current document.$sum
返回分区开始和当前文档之间的文档累积数量。
maximumQuantityForYear
field to the maximum quantity
for each year.maximumQuantityForYear
字段设置为每年的最大数量。Calculates the maximum 使用文档窗口中运行的quantity
of all the documents using the $max
operator run in a documents window.$max
运算符计算所有文档的最大数量。
The window contains documents between an 该窗口包含的文档介于unbounded
lower and upper
limit. unbounded
上限和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: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 }
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
中的一个范围窗口,返回当前文档价格正负10美元范围内的订单销售蛋糕数量值之和:
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { price: 1 }, output: { quantityFromSimilarOrders: { $sum: "$quantity", window: { range: [ -10, 10 ] } } } } } ] )
In the example:在示例中:
partitionBy: "$state"
state
. CA
and WA
.CA
和WA
有分区。sortBy: { price: 1 }
price
in ascending order (1
), so the lowest price
is first.price
升序(1
)对每个分区中的文档进行排序,因此最低price
为第一。output
sets the 将quantityFromSimilarOrders
field to the sum of the quantity
values from the documents in a range window.quantityFromSimilarOrders
字段设置为范围窗口中文档的数量值之和。
-10
and an upper limit of 10
. -10
和上限为10
之间的文档。-10
和10
。$sum
returns the sum of quantity
values contained in a range of plus or minus 10 dollars of the current document's price
value.$sum
返回当前文档price
的正负10美元范围内的数量值之和。In this example output, the sum of the 在此示例输出中,窗口中文档的数量值之和显示在quantity
values for documents in the window is shown in the quantityFromSimilarOrders
field: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 }
The following example uses a window with a positive upper bound time range unit in 以下示例使用了一个以$setWindowFields
. $setWindowFields
为单位的时间范围上限为正的窗口。The pipeline outputs an array of 管道输出与指定时间范围匹配的每个州的orderDate
values for each state
that match the specified time range.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"
state
. CA
and WA
.CA
和WA
有分区。sortBy: { orderDate: 1 }
orderDate
in ascending order (1
), so the earliest orderDate
is first.orderDate
按升序(1
)对每个分区中的文档进行排序,因此最早的orderDate
是第一个。output
:
orderDateArrayForState
array field to orderDate
values for the documents in each state
. orderDateArrayForState
数组字段设置为每个州下文档的orderDate
值。$push
to return an array of orderDate
values from the documents in a range window.$push
从范围窗口中的文档返回orderDate
值数组。unbounded
lower limit and an upper limit set to 10
(10 months after the current document's orderDate
value) using a time range unit.10
(当前文档的orderDate
值后10个月)的unbounded
下限和unbounded
上限之间的文档。$push
orderDate
values for the documents between the beginning of the partition and the documents with orderDate
values inclusively in a range of the current document's orderDate
value plus 10
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" : [ 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") ] }
The following example uses a window with a negative upper bound time range unit in 以下示例使用了一个时间范围单位为负的窗口,单位为$setWindowFields
. $setWindowFields
。The pipeline outputs an array of 管道输出与指定时间范围匹配的每个州的orderDate
values for each state
that match the specified time range.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"
state
. CA
and WA
.CA
和WA
有分区。sortBy: { orderDate: 1 }
orderDate
in ascending order (1
), so the earliest orderDate
is first.orderDate
按升序(1
)对每个分区中的文档进行排序,因此最早的orderDate
是第一个。output
:
orderDateArrayForState
array field to orderDate
values for the documents in each state
. orderDateArrayForState
数组字段设置为每个州下文档的orderDate
值。$push
to return an array of orderDate
values from the documents in a range window.$push
从范围窗口中的文档返回orderDate
值数组。unbounded
lower limit and an upper limit set to -10
(10 months before the current document's orderDate
value) using a time range unit.-10
(当前文档的orderDate
值前10个月)的unbounded
下限和unbounded
上限之间的文档。$push
returns the array of orderDate
values for the documents between the beginning of the partition and the documents with orderDate
values inclusively in a range of the current document's orderDate
value minus 10
months.$push
返回分区开头和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") ] }
For an additional example about IOT Power Consumption, see the Practical MongoDB Aggregations e-book.有关物联网功耗的其他示例,请参阅《实用MongoDB聚合》电子书。