文档菜单
文档首页
/
MongoDB 手册
/ / /

$shift(聚合)

本页

  • 定义
  • 行为
  • 示例

版本5.0.

$shift

返回一个值表达式被应用于相对于当前文档的指定位置的文档,在$setWindowFields阶段分区。

$setWindowFields阶段的sortBy字段值决定了文档的顺序。有关MongoDB如何比较不同类型的字段的信息,请参阅BSON比较顺序。

$shift仅适用于$setWindowFields阶段。

$shift语法

{
$shift: {
output: <output expression>,
by: <integer>,
default: <default expression>
}
}

$shift接受包含以下字段的文档

字段
描述

指定一个表达式以在输出中评估并返回。

指定一个整数,表示相对于输出中当前文档的数值文档位置。

例如

  • 1指定当前文档之后的文档位置。

  • -1指定当前文档之前的文档位置。

  • -2指定当前文档之前两个位置的文档位置。

指定一个可选的默认表达式以在文档位置超出隐式$setWindowFields阶段窗口时评估。隐式窗口包含分区中的所有文档。

默认表达式必须评估为常量值。

如果您未指定默认表达式,则$shift对于位于隐式$setWindowFields阶段窗口之外的文档返回null

$shift窗口 被指定在 $setWindowFields 阶段时将返回一个错误。

创建一个包含加利福尼亚(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 }
] )

下面的示例使用 cakeSales 集合。

此示例使用 $shift$setWindowFields 阶段来输出每个 state 的当前文档之后的蛋糕销售 quantity

db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { quantity: -1 },
output: {
shiftQuantityForState: {
$shift: {
output: "$quantity",
by: 1,
default: "Not available"
}
}
}
}
}
] )

在示例中

  • partitionBy: "$state" state 分区 集合中的文档。有 CAWA 的分区。

  • sortBy: { quantity: -1 } quantity 降序-1)对每个分区的文档进行排序,因此最高 quantity 是第一。

  • sortBy 之后输出

    • shiftQuantityForState 字段设置为每个 state 中文档的 quantity 值。

    • 使用 $shift 返回输出中当前文档之后的文档的 quantity 值。

      • 使用 $shiftby 整数 将其设置为 1 来指定文档位置。

      • 对于隐式 窗口 之外的文档,$shift 返回 "不可用",这通过 默认 表达式指定。

在此示例输出中,偏移后的 数量 值显示在每个返回的文档的 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" }

此示例在 $shift$setWindowFields 阶段中使用,以输出每个 状态 当前文档之前的蛋糕销售数量。

db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { quantity: -1 },
output: {
shiftQuantityForState: {
$shift: {
output: "$quantity",
by: -1,
default: "Not available"
}
}
}
}
}
] )

在示例中

  • partitionBy: "$state" state 分区 集合中的文档。有 CAWA 的分区。

  • sortBy: { quantity: -1 } quantity 降序-1)对每个分区的文档进行排序,因此最高 quantity 是第一。

  • sortBy 之后输出

    • shiftQuantityForState 字段设置为每个 state 中文档的 quantity 值。

    • 使用 $shift 返回输出中当前文档之前的文档的 数量 值。

      • 使用 $shiftby 整数 将其设置为 -1 来指定文档位置。

      • 对于隐式 窗口 之外的文档,$shift 返回 "不可用",这通过 默认 表达式指定。

在此示例输出中,偏移后的 数量 值显示在每个返回的文档的 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 }

返回

$setUnion