$shift (aggregation)
On this page本页内容
Definition定义
New in version 5.0. 5.0版新增。
$shift
Returns the value from an expression applied to a document in a specified position relative to the current document in the 返回应用于文档的表达式中的值,该文档位于$setWindowFields stage partition.$setWindowFields阶段分区中相对于当前文档的指定位置。
The $setWindowFields stage sortBy field value determines the document order. $setWindowFields阶段sortBy字段值确定文档顺序。For more information on how MongoDB compares fields with different types, see BSON comparison order.有关MongoDB如何比较不同类型的字段的更多信息,请参阅BSON比较顺序。
$shift is only available in the 仅在$setWindowFields stage.$setWindowFields阶段中可用。
$shift syntax:语法:
{
$shift: {
output: <output expression>,
by: <integer>,
default: <default expression>
}
}
$shift takes a document with these fields:获取包含以下字段的文档:
output | |
by | integer with a numeric document position relative to the current document in the output.integer,该整数具有相对于输出中当前文档的数字文档位置。
|
default | $setWindowFields stage window. $setWindowFields阶段window之外,则指定要计算的可选默认表达式。default表达式的计算结果必须为常数值。$shift returns null for documents whose positions are outside of the implicit $setWindowFields stage window. default表达式,则对于位置在隐式$setWindowFields阶段窗口之外的文档,$shift将返回null。 |
Behavior行为
$shift returns an error if you specify a window in the 如果在$setWindowFields stage.$setWindowFields阶段中指定window,则返回错误。
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 cakeSales collection is used in the following examples.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:$setWindowFields阶段中的$shift来输出每个state下当前文档后面的每个文档的蛋糕销售quantity:
db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { quantity: -1 },
output: {
shiftQuantityForState: {
$shift: {
output: "$quantity",
by: 1,
default: "Not available"
}
}
}
}
}
] )
In the example:在示例中:
partitionBy: "$state"partitions the documents in the collection by按state.state对集合中的文档进行分区。There are partitions forCAandWA.CA和WA有分区。sortBy: { quantity: -1 }sorts the documents in each partition by按quantityin descending order (-1), so the highestquantityis first.quantity降序(-1)对每个分区中的文档进行排序,因此quantity最高的是第一个。
outputaftersortBy:sortBy之后的输出:Sets the将shiftQuantityForStatefield to thequantityvalue from the documents in eachstate.shiftQuantityForState字段设置为每个state下文档的quantity值。Uses使用$shiftto return thequantityvalue from the document that follows the current document in the output.$shift返回输出中当前文档后面的文档的quantity值。
In this example output, the shifted 在本示例输出中,每个返回文档的quantity value is shown in the shiftQuantityForState field for each returned document:shiftQuantityForState字段中显示了偏移的quantity值:
{ "_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:$setWindowFields阶段中的$shift来输出每个state的当前文档之前的每个文档的蛋糕销售quantity:
db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { quantity: -1 },
output: {
shiftQuantityForState: {
$shift: {
output: "$quantity",
by: -1,
default: "Not available"
}
}
}
}
}
] )
In the example:在示例中:
partitionBy: "$state"partitions the documents in the collection by按state.state对集合中的文档进行分区。There are partitions forCAandWA.CA和WA有分区。sortBy: { quantity: -1 }sorts the documents in each partition by按quantityin descending order (-1), so the highestquantityis first.quantity降序(-1)对每个分区中的文档进行排序,因此quantity最高的是第一个。
outputaftersortBy:sortBy之后的输出:Sets the将shiftQuantityForStatefield to thequantityvalue from the documents in eachstate.shiftQuantityForState字段设置为每个state下文档的quantity值。Uses使用$shiftto return thequantityvalue from the document before the current document in the output.$shift返回输出中当前文档之前文档的quantity值。
In this example output, the shifted 在本示例输出中,每个返回文档的quantity value is shown in the shiftQuantityForState field for each returned document:shiftQuantityForState字段中显示了偏移的quantity值:
{ "_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 }