On this page本页内容
New in version 5.0.在版本5.0中新增。
Returns the document position (known as the rank) relative to other documents in the 返回相对于$setWindowFields
stage partition.$setWindowFields
阶段分区中其他文档的文档位置(称为排名)。
The $setWindowFields
stage sortBy field value determines the document rank. $setWindowFields
阶段sortBy
字段值确定文档排名。For more information on how MongoDB compares fields with different types, see BSON comparison order.有关MongoDB如何比较不同类型字段的更多信息,请参阅BSON比较顺序。
If multiple documents occupy the same rank, 如果多个文档占据同一排名,$rank
places the document with the subsequent value at a rank with a gap (see Behavior).$rank
将具有后续值的文档放置在具有间隙的排名(请参见行为)。
$rank
is only available in the 仅在$setWindowFields
stage.$setWindowFields
阶段可用。
$rank
syntax:语法:
{ $rank: { } }
$rank
does not accept any parameters.不接受任何参数。
$rank
and $denseRank
differ in how they rank duplicate sortBy field values. $rank
和$denseRank
的不同之处在于它们对重复的sortBy
字段值进行排序的方式。For example, with sortBy field values of 7, 9, 9, and 10:例如,sortBy
字段值为7、9、9和10:
$denseRank
$rank
Documents with a null
value for a sortBy field or documents missing the sortBy field are assigned a rank based on the BSON comparison order.sortBy
字段值为null
的文档或缺少sortBy
字段的文档将根据BSON比较顺序分配一个排名。
See the example in Rank Partitions Containing Duplicate Values, Nulls, or Missing Data.请参见包含重复值、空值或丢失数据的排序分区中的示例。
Create a 创建包含加利福尼亚州(cakeSales
collection that contains cake sales in the states of California (CA
) and Washington (WA
):CA
)和华盛顿州(WA
)蛋糕销售的cakeSales
集合:
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 } ] )
This example uses 此示例使用$rank
in the $setWindowFields
stage to output the quantity
rank of the cake sales for each state
:$setWindowFields
阶段中的$rank
输出每个州的蛋糕销售数量排名:
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { quantity: -1 }, output: { rankQuantityForState: { $rank: {} } } } } ] )
In the example:在示例中:
partitionBy: "$state"
state
. There are partitions for CA
and WA
.state
划分集合中的文档。CA
和WA
有分区。sortBy: { quantity: -1 }
quantity
in descending order (-1
), so the highest quantity
is first.quantity
按降序(-1
)对每个分区中的文档进行排序,因此最高的quantity
是第一个。output
sets the rankQuantityForState
field to the quantity
rank using $rank
, as shown in the following results.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 }
This example shows how to use dates with 此示例显示如何在$rank
in the $setWindowFields
stage to output the orderDate
rank of the cake sales for each state
:$setWindowFields
阶段中使用带有$rank
的日期来输出每个state
蛋糕销售的orderDate
排名:
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { orderDate: 1 }, output: { rankOrderDateForState: { $rank: {} } } } } ] )
In the example:在示例中:
partitionBy: "$state"
state
. state
划分集合中的文档。CA
and WA
.CA
和WA
有分区。sortBy: { orderDate: 1 }
orderDate
in ascending order (1
), so the earliest orderDate
is first.orderDate按
升序(1
)对每个分区中的文档进行排序,因此最早的orderDate
是第一个。output
sets the rankOrderDateForState
field to the orderDate
rank using $rank
, as shown in the following results.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 }
Create a 创建一个cakeSalesWithDuplicates
collection where:cakeSalesWithDuplicates
集合,其中:
CA
) and Washington (WA
).CA
)和华盛顿州(WA
)。quantity
and state
as document 5.quantity
和state
与文件5相同。quantity
and state
as document 4.quantity
和state
。null
quantity
.quantity
为null
。quantity
.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 } ] )
This example uses 此示例使用$rank
in the $setWindowFields
stage to output the quantity
rank from the cakeSalesWithDuplicates
collection for each state
:$setWindowFields
阶段中的$rank
从每个state
的cakeSalesWithDuplicates
集合中输出quantity
排名:
db.cakeSalesWithDuplicates.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { quantity: -1 }, output: { rankQuantityForState: { $rank: {} } } } } ] )
In the example:在示例中:
partitionBy: "$state"
state
. state
划分集合中的文档。CA
and WA
.CA
和WA
有分区。sortBy: { quantity: -1 }
quantity
in descending order (-1
), so the highest quantity
is first.quantity
按降序(-1
)对每个分区中的文档进行排序,因此最高的quantity
是第一个。output
sets the rankOrderDateForState
field to the quantity
rank using $rank
.output
使用$rank
将rankOrderDateForState
字段设置为数量等级。In the following example output:在以下输出示例中:
quantity
and state
have the same rank. quantity
和state
相同的文档具有相同的等级。null
quantity
and then the document with the missing quantity
are ranked the lowest in the output for the CA
partition. quantity
为null
的文档和quantity
缺失的文档在CA
分区的输出中排名最低。null
and missing values after number values in this example.{ "_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 }