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

$documentNumber (聚合)

本页内容

  • 定义
  • 行为
  • 示例

版本中5.0.

$documentNumber

返回文档(称为文档编号)在$setWindowFields阶段分区中的位置。

$setWindowFields阶段的sortBy字段确定文档编号。有关MongoDB比较不同类型字段的方式的更多信息,请参阅BSON比较顺序

$documentNumber为分区中的每个文档返回一个唯一的数字,即使分区中有多个文档具有相同的sortBy字段值。

$documentNumber仅在$setWindowFields阶段中可用。

$documentNumber语法

{ $documentNumber: { } }

$documentNumber不接受任何参数。

$documentNumber 包括具有 null 或缺失的 sortBy 字段的文档。

$documentNumber$rank$denseRank 根据文档的 sortBy 字段值返回文档的位置。

$documentNumber$rank$denseRank 在处理具有相同的 sortBy 字段值在同一 partition 的文档时有所不同。

  • $rank$denseRank 为这些文档返回相同的位置(称为排名)。

  • $documentNumber 为这些文档返回一个独特的位置(称为文档编号)。

请参阅重复、空和缺失值文档编号的示例。

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

此示例在 $documentNumber 中使用 $setWindowFields 阶段以输出每个 state 的糕点销售文档编号

db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { quantity: -1 },
output: {
documentNumberForState: {
$documentNumber: {}
}
}
}
}
] )

在示例中

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

  • sortBy: { quantity: -1 } 排序 每个分区中的文档,按 quantity 降序排列(-1),因此最高的 quantity 最先。

  • output 在新字段 documentNumberForState 中设置文档编号,如下所示。 documentNumberForState 在每个 state 分区中是唯一的。

{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"),
"state" : "CA", "price" : 41, "quantity" : 162, "documentNumberForState" : 1 }
{ "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"),
"state" : "CA", "price" : 12, "quantity" : 145, "documentNumberForState" : 2 }
{ "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"),
"state" : "CA", "price" : 13, "quantity" : 120, "documentNumberForState" : 3 }
{ "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"),
"state" : "WA", "price" : 14, "quantity" : 140, "documentNumberForState" : 1 }
{ "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"),
"state" : "WA", "price" : 43, "quantity" : 134, "documentNumberForState" : 2 }
{ "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"),
"state" : "WA", "price" : 13, "quantity" : 104, "documentNumberForState" : 3 }

创建一个 cakeSalesWithDuplicates 集合,其中

  • 蛋糕销售处于加利福尼亚州(CA)和华盛顿州(WA)。

  • 文档 6 到 8 与文档 5 具有相同的 quantitystate

  • 文档 9 与文档 4 具有相同的 quantitystate

  • 文档 10 的 quantitynull

  • 文档 11 缺失 quantity

db.cakeSalesWithDuplicates.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 },
{ _id: 6, type: "strawberry", orderDate: new Date("2020-01-08T06:12:03Z"),
state: "WA", price: 41, quantity: 134 },
{ _id: 7, type: "strawberry", orderDate: new Date("2020-01-01T06:12:03Z"),
state: "WA", price: 34, quantity: 134 },
{ _id: 8, type: "strawberry", orderDate: new Date("2020-01-02T06:12:03Z"),
state: "WA", price: 40, quantity: 134 },
{ _id: 9, type: "strawberry", orderDate: new Date("2020-05-11T16:09:01Z"),
state: "CA", price: 39, quantity: 162 },
{ _id: 10, type: "strawberry", orderDate: new Date("2020-05-11T16:09:01Z"),
state: "CA", price: 39, quantity: null },
{ _id: 11, type: "strawberry", orderDate: new Date("2020-05-11T16:09:01Z"),
state: "CA", price: 39 }
] )

此示例在 $documentNumber 阶段使用 $setWindowFields 以输出每个 statecakeSalesWithDuplicates 文档编号

db.cakeSalesWithDuplicates.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { quantity: -1 },
output: {
documentNumberForState: {
$documentNumber: {}
}
}
}
}
] )

在示例中

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

  • sortBy: { quantity: -1 } 排序 每个分区中的文档,按 quantity 降序排列(-1),因此最高的 quantity 最先。

  • output 将文档编号设置在新字段 documentNumberForState 中,如下所示。在 state 分区中,documentNumberForState 是唯一的,并为具有 null 和缺失 quantity 的文档设置 documentNumberForState 值。

{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"),
"state" : "CA", "price" : 41, "quantity" : 162, "documentNumberForState" : 1 }
{ "_id" : 9, "type" : "strawberry", "orderDate" : ISODate("2020-05-11T16:09:01Z"),
"state" : "CA", "price" : 39, "quantity" : 162, "documentNumberForState" : 2 }
{ "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"),
"state" : "CA", "price" : 12, "quantity" : 145, "documentNumberForState" : 3 }
{ "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"),
"state" : "CA", "price" : 13, "quantity" : 120, "documentNumberForState" : 4 }
{ "_id" : 10, "type" : "strawberry", "orderDate" : ISODate("2020-05-11T16:09:01Z"),
"state" : "CA", "price" : 39, "quantity" : null, "documentNumberForState" : 5 }
{ "_id" : 11, "type" : "strawberry", "orderDate" : ISODate("2020-05-11T16:09:01Z"),
"state" : "CA", "price" : 39, "documentNumberForState" : 6 }
{ "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"),
"state" : "WA", "price" : 14, "quantity" : 140, "documentNumberForState" : 1 }
{ "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"),
"state" : "WA", "price" : 43, "quantity" : 134, "documentNumberForState" : 2 }
{ "_id" : 6, "type" : "strawberry", "orderDate" : ISODate("2020-01-08T06:12:03Z"),
"state" : "WA", "price" : 41, "quantity" : 134, "documentNumberForState" : 3 }
{ "_id" : 7, "type" : "strawberry", "orderDate" : ISODate("2020-01-01T06:12:03Z"),
"state" : "WA", "price" : 34, "quantity" : 134, "documentNumberForState" : 4 }
{ "_id" : 8, "type" : "strawberry", "orderDate" : ISODate("2020-01-02T06:12:03Z"),
"state" : "WA", "price" : 40, "quantity" : 134, "documentNumberForState" : 5 }
{ "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"),
"state" : "WA", "price" : 13, "quantity" : 104, "documentNumberForState" : 6 }

返回

除法

本页内容