$shift (aggregation)
定义
版本 5.0 中的新增功能。
Returns the value from an 表达式(expression)
applied to a document in a specified position relative to the current
document in the $setWindowFields
stage partition.
The $setWindowFields
stage sortBy field value determines the document order. For
more information on how MongoDB compares fields with different types,
see BSON comparison order.
$shift
只能在 $setWindowFields
阶段使用。
$shift
语法:
{ $shift: { output: <output expression>, by: <integer>, default: <default expression> } }
$shift
接受包含以下字段的文档:
字段 | 说明 |
---|---|
Specifies an 表达式(expression) to evaluate and return in the output. | |
Specifies an 例如:
| |
Specifies an optional default 表达式(expression) to evaluate if the document position
is outside of the implicit The 访问 expression must evaluate to a constant value. If you do not specify a 访问
expression, |
行为
$shift
returns an error if you specify a 窗口 in the $setWindowFields
stage.
示例
创建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 } ] )
以下示例中使用了 cakeSales
集合。
Shift Using a Positive Integer
This example uses $shift
in the $setWindowFields
stage to output the quantity
of the cake sales from each document
following the current document for each state
:
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { quantity: -1 }, output: { shiftQuantityForState: { $shift: { output: "$quantity", by: 1, default: "Not available" } } } } } ] )
在示例中:
partitionBy: "$state"
按state
对集合中的文档分区。
CA
和WA
都有分区。sortBy: { quantity: -1 }
按quantity
以降序 (-1
) 对每个分区中的文档进行排序,因此最高的
quantity
位于最前面。
output
aftersortBy
:Sets the
shiftQuantityForState
field to thequantity
value from the documents in eachstate
.Uses
$shift
to return thequantity
value from the document that follows the current document in the output.
In this example output, the shifted quantity
value is shown in the
shiftQuantityForState
field for each returned document:
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"), "state" : "CA", "price" : 41, "quantity" : 162, "shiftQuantityForState" : 145 } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"), "state" : "CA", "price" : 12, "quantity" : 145, "shiftQuantityForState" : 120 } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"), "state" : "CA", "price" : 13, "quantity" : 120, "shiftQuantityForState" : "Not available" } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"), "state" : "WA", "price" : 14, "quantity" : 140, "shiftQuantityForState" : 134 } { "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"), "state" : "WA", "price" : 43, "quantity" : 134, "shiftQuantityForState" : 104 } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"), "state" : "WA", "price" : 13, "quantity" : 104, "shiftQuantityForState" : "Not available" }
Shift Using a Negative Integer
This example uses $shift
in the $setWindowFields
stage to output the quantity
of the cake sales from each document
before the current document for each state
:
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { quantity: -1 }, output: { shiftQuantityForState: { $shift: { output: "$quantity", by: -1, default: "Not available" } } } } } ] )
在示例中:
partitionBy: "$state"
按state
对集合中的文档分区。
CA
和WA
都有分区。sortBy: { quantity: -1 }
按quantity
以降序 (-1
) 对每个分区中的文档进行排序,因此最高的
quantity
位于最前面。
output
aftersortBy
:Sets the
shiftQuantityForState
field to thequantity
value from the documents in eachstate
.Uses
$shift
to return thequantity
value from the document before the current document in the output.
In this example output, the shifted quantity
value is shown in the
shiftQuantityForState
field for each returned document:
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"), "state" : "CA", "price" : 41, "quantity" : 162, "shiftQuantityForState" : "Not available" } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"), "state" : "CA", "price" : 12, "quantity" : 145, "shiftQuantityForState" : 162 } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"), "state" : "CA", "price" : 13, "quantity" : 120, "shiftQuantityForState" : 145 } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"), "state" : "WA", "price" : 14, "quantity" : 140, "shiftQuantityForState" : "Not available" } { "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"), "state" : "WA", "price" : 43, "quantity" : 134, "shiftQuantityForState" : 140 } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"), "state" : "WA", "price" : 13, "quantity" : 104, "shiftQuantityForState" : 134 }