Docs 菜单
Docs 主页
/
MongoDB Manual
/ / /

$rank(聚合)

在此页面上

  • 定义
  • 行为
  • 示例

版本 5.0 中的新增功能

$rank

返回文档在 $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 对集合中的文档分区CAWA 都有分区。

  • sortBy: { quantity: -1 }quantity 以降序 (-1) 对每个分区中的文档进行排序,因此最高的 quantity 位于最前面。

  • output 使用$rankrankQuantityForState字段设置为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 对集合中的文档分区CAWA 都有分区。

  • sortBy: { orderDate: 1 }orderDate 以升序 (1) 对每个分区中的文档进行排序,因此最早的 orderDate 位于最前面。

  • output 使用$rankrankOrderDateForState字段设置为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 具有相同的 quantitystate

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

  • 文档 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 集合中输出每个 statequantity 排名:

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

在示例中:

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

  • sortBy: { quantity: -1 }quantity 以降序 (-1) 对每个分区中的文档进行排序,因此最高的 quantity 位于最前面。

  • output 使用 $rankrankOrderDateForState字段设置为quantity排名

在以下示例输出中:

  • 具有相同 quantitystate 的文档具有相同的排名。如果多份文档排名相同,则该排名与下个排名之间存在差距。

  • 包含 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 }

后退

$range

在此页面上