“文档” 菜单
文档首页
/
MongoDB Manual
/ / /

$shift(聚合)

在此页面上

  • 定义
  • 行为
  • 举例

版本 5.0 中的新增功能

$shift

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

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

$shift 只能在 $setWindowFields 阶段使用。

$shift 事务语法:

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

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

字段
说明
输出

指定要计算并在输出中返回的表达式

在输出中指定一个带有相对于当前文档的数字文档位置的integer

例如:

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

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

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

指定一个可选的默认表达式,用于计算文档位置是否位于隐式$setWindowFields阶段窗口之外。 隐式窗口包含分区中的所有文档。

默认表达式的计算结果必须为常量值。

如果不指定 默认 表达式,则对于位置超出隐式 阶段窗口的文档,$shift 会返回null $setWindowFields

$shift如果您在 阶段指定 窗口 , 会返回错误。$setWindowFields

创建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 $setWindowFields阶段使用quantity state,为每个

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 位于最前面。

  • output sortBy之后:

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

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

      • 文档位置由设置为$shift integer1的 使用 指定。

      • 对于隐式窗口之外的文档, $shift返回"Not available" ,这是使用默认表达式指定的。

在此示例输出中,移动后的quantity值显示在每个返回文档的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阶段使用quantity state为每个

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 位于最前面。

  • output sortBy之后:

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

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

      • 文档位置由设置为$shift integer-1的 使用 指定。

      • 对于隐式窗口之外的文档, $shift返回"Not available" ,这是使用默认表达式指定的。

在此示例输出中,移动后的quantity值显示在每个返回文档的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(聚合)

在此页面上