MongoDB聚合运算符:$rank
文章目录
- [MongoDB聚合运算符:rank](#MongoDB聚合运算符:rank)
$rank
聚合运算符返回$setWindowFields
阶段分区内文档相对排名位置,文档排名先后顺序有$setWindowFields
阶段的sortBy
字段决定,sortBy
只能取一个字段值。另外,如果多个文档有相同的排名,$rank
会将文档与后续值放在排名的空隙中。
语法
js
{ $rank: { } }
$rank
不接受任何参数。
使用
$rank
和$denseRank
的不同之处在于它们对sortBy
字段重复值的处理不同,例如,sortField的值有7、9、9、10:
$denseRank
排名的值为1、2、2、3,重复值9的排名为2,而10的排名为3,没有排名间隙。$rank
排名的值为1、2、2、4,重复值9的排名为为2,但10的排名为4,这里存在一个间隙3。
对于文档的sortBy
字段值为空或缺失的情况,sortBy
字段相关的排名按照BSON比较顺序执行。
举例
使用下面的脚本创建cakeSales
集合,包含了在加利福尼亚(CA)和华盛顿(WA)的蛋糕销售记录:
js
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
输出每个州的蛋糕销售量quantity
排名:
js
db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { quantity: -1 },
output: {
rankQuantityForState: {
$rank: {}
}
}
}
}
] )
在本例中:
partitionBy: "$state"
根据state
对集合中的文档进行分区,分为两个区CA
和WA
。sortBy: {quantity: -1 }
根据quantity
对分区内的文档由大到小进行排序,销量最高的quantity
排在最前面。output
使用$rank
将rankQuantityForState
字段设置为quantity
排名。
操作返回下面的结果:
json
{ "_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
输出每个州蛋糕销售的orderDate
排名:
js
db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { orderDate: 1 },
output: {
rankOrderDateForState: {
$rank: {}
}
}
}
}
] )
在本例中:
partitionBy: "$state"
根据state
对集合中的文档进行分区,分为两个区CA
和WA
。sortBy: { orderDate: 1 }
根据orderDate
对分区内的文档由小到大进行排序,最早的orderDate
排在最前面。output
使用$rank
对窗口中的文档按照销售日期进行排名。
操作返回下面的结果:
json
{ "_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
集合:
js
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 }
] )
在cakeSalesWithDuplicates
集合中:
- 蛋糕销售的地点有加利福尼亚州(CA)和华盛顿州(WA)
- 文档6到8与文档5的
quantity
和state
相同 - 文档9与文档4的
quantity
和state
相同 - 文档10的
quantity
为null
- 文档11的
quantity
字段缺失
下面的例子在$setWindowFields
阶段使用$rank
依据quantity
对cakeSalesWithDuplicates
集合文档进行排名:
js
db.cakeSalesWithDuplicates.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { quantity: -1 },
output: {
rankQuantityForState: {
$rank: {}
}
}
}
}
] )
partitionBy: "state"
依据state
字段对文档进行分区,有CA
和WA
两个分区sortBy:{quantity:-1}
依据quantity
对分区内的文档按照从大到小进行排序,quantity
最大的排在最前面output
使用$rank
将quantity
字段的排名赋予denseRankOrderDateForState
字段,结果如下:
json
{ "_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 }
从上面的结果可以看出:
- 数量和状态相同的文件具有相同的排名,如果文档具有相同的排名,则该排名与下一个排名之间存在差距。
- 在 CA 分区的输出中,数量为空的文档和数量为缺失的文档排序最低。这种排序是 BSON 比较顺序的结果,在本例中,将空值和缺失值排序在数字值之后。