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