$documentNumber(聚合)
定义
版本 5.0 中的新增功能。
返回文档在 $setWindowFields
阶段分区中的位置(称为文档编号)。
$setWindowFields
阶段 sortBy 字段决定文档编号。有关 MongoDB 如何比较不同类型字段的更多信息,请参阅 BSON 比较顺序。
$documentNumber
会为分区中的每个文档返回一个唯一的数字,即使多个文档在分区中具有相同的 SortBy 字段值也是如此。
$documentNumber
只能在 $setWindowFields
阶段使用。
$documentNumber
事务语法:
{ $documentNumber: { } }
$documentNumber
不接受任何参数。
行为
$documentNumber
包括 sortBy 字段为 null
或缺失的文档。
$documentNumber
、$rank
和 $denseRank
根据 sortBy 字段值返回文档的位置。
$documentNumber
与 $rank
和 $denseRank
的不同之处在于如何处理分区中具有相同 sortBy 字段值的文档:
$rank
和$denseRank
返回这些文档的相同位置(称为等级)。$documentNumber
返回这些文档的唯一位置(称为文档编号)。
请参阅重复值、Null 和缺失值的文档编号中的示例。
示例
每个州的文件编号
创建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 } ] )
此示例在 $setWindowFields
阶段使用 $documentNumber
来输出每个 state
的蛋糕销售单据编号:
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { quantity: -1 }, output: { documentNumberForState: { $documentNumber: {} } } } } ] )
在示例中:
partitionBy: "$state"
按state
对集合中的文档分区。CA
和WA
都有分区。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 }
重复值、Null 和缺失值的文档编号
创建 cakeSalesWithDuplicates
集合,其中:
蛋糕销售位于加利福尼亚州 (
CA
) 和华盛顿州 (WA
)。文档 6 至 8 与文档 5 具有相同的
quantity
和state
。文档 9 具有与文档 4 相同的
quantity
和state
。文档 10 包含
null
quantity
。文档 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 } ] )
此示例在 $setWindowFields
阶段使用 $documentNumber
来输出每个 state
的 cakeSalesWithDuplicates
文档编号:
db.cakeSalesWithDuplicates.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { quantity: -1 }, output: { documentNumberForState: { $documentNumber: {} } } } } ] )
在示例中:
partitionBy: "$state"
按state
对集合中的文档分区。CA
和WA
都有分区。sortBy: { quantity: -1 }
按quantity
以降序 (-1
) 对每个分区中的文档进行排序,因此最高的quantity
位于最前面。
output
在名为documentNumberForState
的新字段中设置文档编号,如以下结果所示。documentNumberForState
在每个state
分区中都是唯一的,并且具有null
quantity
和缺少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 }