$denseRank(聚合)
定义
版本 5.0 中的新增功能。
返回文档在 $setWindowFields
阶段分区中相对于其他文档的位置(称为排名)。
$setWindowFields
阶段 sortBy 字段值决定文档排名。有关 MongoDB 如何比较不同类型字段的更多信息,请参阅 BSON 比较顺序。
如果多个文档占据相同的排名,则 $denseRank
会将具有后续值的文档无间隔地置于下一排名(请参阅“行为”)。
$denseRank
只能在 $setWindowFields
阶段使用。
$denseRank
事务语法:
{ $denseRank: { } }
$denseRank
不接受任何参数。
行为
$rank
和$denseRank
的不同之处在于对重复的 sortBy字段值进行排名的方式。 例如, sortBy字段值为7 、 9 、 9和10 :
$denseRank
对这些值排列名次,为 1、2、2 和 3。重复的 9 个值的排名为 2,重复的 10 个值的排名为 3。这些排名没有差异。$rank
对这些值排列名次,为 1、2、2 和 4。重复的 9 个值的排名为 2,而 10 的排名为 4。排名 3 存在差异。
示例
按整数字段的密集排名分区
创建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 } ] )
此示例在$denseRank
$setWindowFields
阶段使用quantity
输出每个 蛋糕销售的state
密集排名:
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { quantity: -1 }, output: { denseRankQuantityForState: { $denseRank: {} } } } } ] )
在示例中:
partitionBy: "$state"
按state
对集合中的文档分区。CA
和WA
都有分区。sortBy: { quantity: -1 }
按quantity
以降序 (-1
) 对每个分区中的文档进行排序,因此最高的quantity
位于最前面。
output
使用$denseRank
将denseRankOrderDateForState
字段设置为orderDate
密集秩,如以下结果所示。
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"), "state" : "CA", "price" : 41, "quantity" : 162, "denseRankQuantityForState" : 1 } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"), "state" : "CA", "price" : 12, "quantity" : 145, "denseRankQuantityForState" : 2 } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"), "state" : "CA", "price" : 13, "quantity" : 120, "denseRankQuantityForState" : 3 } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"), "state" : "WA", "price" : 14, "quantity" : 140, "denseRankQuantityForState" : 1 } { "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"), "state" : "WA", "price" : 43, "quantity" : 134, "denseRankQuantityForState" : 2 } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"), "state" : "WA", "price" : 13, "quantity" : 104, "denseRankQuantityForState" : 3 }
按日期字段密集排名分区
此示例展示了如何在$denseRank
$setWindowFields
阶段使用带有orderDate
的日期来输出每个 蛋糕销售的state
密集排名:
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { orderDate: 1 }, output: { denseRankOrderDateForState: { $denseRank: {} } } } } ] )
在示例中:
partitionBy: "$state"
按state
对集合中的文档分区。CA
和WA
都有分区。sortBy: { orderDate: 1 }
按orderDate
以升序 (1
) 对每个分区中的文档进行排序,因此最早的orderDate
位于最前面。
output
使用$denseRank
将denseRankOrderDateForState
字段设置为orderDate
排名,如以下结果所示。
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"), "state" : "CA", "price" : 41, "quantity" : 162, "denseRankOrderDateForState" : 1 } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"), "state" : "CA", "price" : 13, "quantity" : 120, "denseRankOrderDateForState" : 2 } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"), "state" : "CA", "price" : 12, "quantity" : 145, "denseRankOrderDateForState" : 3 } { "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"), "state" : "WA", "price" : 43, "quantity" : 134, "denseRankOrderDateForState" : 1 } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"), "state" : "WA", "price" : 13, "quantity" : 104, "denseRankOrderDateForState" : 2 } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"), "state" : "WA", "price" : 14, "quantity" : 140, "denseRankOrderDateForState" : 3 }
重复值、空值和缺失值的密集排名
创建 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 } ] )
此示例在$denseRank
$setWindowFields
阶段使用quantity
从cakeSalesWithDuplicates
集合中输出每个 的state
密集秩:
db.cakeSalesWithDuplicates.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { quantity: -1 }, output: { denseRankQuantityForState: { $denseRank: {} } } } } ] )
在示例中:
partitionBy: "$state"
按state
对集合中的文档分区。CA
和WA
都有分区。sortBy: { quantity: -1 }
按quantity
以降序 (-1
) 对每个分区中的文档进行排序,因此最高的quantity
位于最前面。
在以下示例输出中:
具有相同
quantity
和state
的文档具有相同的排名,并且排名之间没有差距。 这与$rank
不同,后者在排名之间有差距(有关示例,请参阅包含重复值、空值或缺失数据的排名分区)。包含
null
quantity
的文档以及缺失quantity
的文档在CA
分区的输出中排名最低。这种排序是按照 BSON 比较顺序分配排名的结果,在本示例中,BSON 将null
和缺失值排序在数字值之后。
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"), "state" : "CA", "price" : 41, "quantity" : 162, "denseRankQuantityForState" : 1 } { "_id" : 9, "type" : "strawberry", "orderDate" : ISODate("2020-05-11T16:09:01Z"), "state" : "CA", "price" : 39, "quantity" : 162, "denseRankQuantityForState" : 1 } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"), "state" : "CA", "price" : 12, "quantity" : 145, "denseRankQuantityForState" : 2 } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"), "state" : "CA", "price" : 13, "quantity" : 120, "denseRankQuantityForState" : 3 } { "_id" : 10, "type" : "strawberry", "orderDate" : ISODate("2020-05-11T16:09:01Z"), "state" : "CA", "price" : 39, "quantity" : null, "denseRankQuantityForState" : 4 } { "_id" : 11, "type" : "strawberry", "orderDate" : ISODate("2020-05-11T16:09:01Z"), "state" : "CA", "price" : 39, "denseRankQuantityForState" : 5 } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"), "state" : "WA", "price" : 14, "quantity" : 140, "denseRankQuantityForState" : 1 } { "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"), "state" : "WA", "price" : 43, "quantity" : 134, "denseRankQuantityForState" : 2 } { "_id" : 6, "type" : "strawberry", "orderDate" : ISODate("2020-01-08T06:12:03Z"), "state" : "WA", "price" : 41, "quantity" : 134, "denseRankQuantityForState" : 2 } { "_id" : 7, "type" : "strawberry", "orderDate" : ISODate("2020-01-01T06:12:03Z"), "state" : "WA", "price" : 34, "quantity" : 134, "denseRankQuantityForState" : 2 } { "_id" : 8, "type" : "strawberry", "orderDate" : ISODate("2020-01-02T06:12:03Z"), "state" : "WA", "price" : 40, "quantity" : 134, "denseRankQuantityForState" : 2 } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"), "state" : "WA", "price" : 13, "quantity" : 104, "denseRankQuantityForState" : 3 }