$shift聚合运算符用于计算在
$setWindowField阶段分区内指定位置文档的表达式的值。
$setWindowFields阶段
sortBy字段值决定文档顺序。
shift只能用于
$setWindowFields阶段。
{
$shift: {
output: <output expression>,
by: <integer>,
default: <default expression>
}
}
字段说明:
output:指定要计算并在输出中返回的表达式。by:整数,指定输出中相对于当前文档的位置。例如:1表示当前文档后面的文档;-1表示当前文档前面的文档;-2表示当前文档前面的第2个文档。default:指定一个可选的默认表达式,以评估文档位置是否位于隐式 $setWindowFields 阶段窗口之外。隐式窗口包含分区中的所有文档。默认表达式的计算结果必须为常量值。如果不指定默认表达式,则对于位于隐式 $setWindowFields 阶段窗口之外的文档,$shift 将返回 null。$setWindowFields 阶段指定窗口,$shift 将返回错误信息。使用下面的脚本创建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 }
] )
下面的聚合在 $setWindowFields 阶段使用 $shift 来输出每个州当前文档之后的每个文档的蛋糕销售数量:
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对分区内的文档按照由大到小进行排序,最大的quantity排在最前面。output在sortBy之后:
shiftQuantityForState 字段设置为各州文档的数量值。$shift 返回输出中当前文档后面文档的数量值。
1 的整数 $shift 来指定。$shift 返回"Not available",这是使用默认表达式指定的。在本例输出中,每个返回文档的 shiftQuantityForState 字段中都显示了移动后的数量值:
{ "_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" }
下面的聚合在 $setWindowFields 阶段使用 $shift 来输出每个州当前文档之前的每个文档的蛋糕销售数量:
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对分区内的文档按照由大到小进行排序,最大的quantity排在最前面。output在sortBy之后:
shiftQuantityForState 字段设置为各州文档的数量值。$shift 返回输出中当前文档后面文档的数量值。
-1 的整数 $shift 来指定。$shift 返回"Not available",这是使用默认表达式指定的。在本例输出中,每个返回文档的 shiftQuantityForState 字段中都显示了移动后的数量值:
{ "_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 }