View a markdown version of this page

$denseRank - Amazon DocumentDB

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

$denseRank

8.0.2 版中的新内容。

Amazon DocumentDB 中的$denseRank窗口运算符根据$setWindowFields阶段sortBy顺序返回每个文档在其分区中的位置。sortBy值相同的文档获得相同的排名,下一个不同的值使用紧随其后的位置,不留空白(例如,1, 2, 2, 3)。它是在$setWindowFields舞台的output字段下指定的。

它是一个仅在$setWindowFields舞台中使用的窗口运算符。

参数

  • $denseRank将空文档{}作为其值。

  • 它不接受window框架。

  • 在$setWindowFields舞台中只需要一把sortBy钥匙。

示例(MongoDB Shell)

以下示例按文档分区game,按降序对每个分区进行score排序,并为每个玩家分配密集等级。

创建示例文档

db.players.insertMany([ { _id: 1, game: "A", player: "Alice", score: 90 }, { _id: 2, game: "A", player: "Bob", score: 85 }, { _id: 3, game: "A", player: "Carol", score: 85 }, { _id: 4, game: "A", player: "Dave", score: 70 }, { _id: 5, game: "B", player: "Erin", score: 95 }, { _id: 6, game: "B", player: "Frank", score: 80 } ]);

查询示例

db.players.aggregate([ { $setWindowFields: { partitionBy: "$game", sortBy: { score: -1 }, output: { denseRank: { $denseRank: {} } } } } ]);

输出

[ { "_id": 1, "game": "A", "player": "Alice", "score": 90, "denseRank": 1 }, { "_id": 2, "game": "A", "player": "Bob", "score": 85, "denseRank": 2 }, { "_id": 3, "game": "A", "player": "Carol", "score": 85, "denseRank": 2 }, { "_id": 4, "game": "A", "player": "Dave", "score": 70, "denseRank": 3 }, { "_id": 5, "game": "B", "player": "Erin", "score": 95, "denseRank": 1 }, { "_id": 6, "game": "B", "player": "Frank", "score": 80, "denseRank": 2 } ]

在每个game分区中,玩家按降序score排列。鲍勃和卡罗尔并列85并列并列排名2,戴夫紧随3其后,没有差距。

代码示例

要查看在$setWindowFields阶段中使用$denseRank运算符的代码示例,请选择要使用的语言的选项卡:

Node.js
const { MongoClient } = require('mongodb'); async function example() { const client = new MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); try { await client.connect(); const db = client.db('test'); const players = db.collection('players'); await players.insertMany([ { _id: 1, game: "A", player: "Alice", score: 90 }, { _id: 2, game: "A", player: "Bob", score: 85 }, { _id: 3, game: "A", player: "Carol", score: 85 }, { _id: 4, game: "A", player: "Dave", score: 70 }, { _id: 5, game: "B", player: "Erin", score: 95 }, { _id: 6, game: "B", player: "Frank", score: 80 } ]); const result = await players.aggregate([ { $setWindowFields: { partitionBy: "$game", sortBy: { score: -1 }, output: { denseRank: { $denseRank: {} } } } } ]).toArray(); console.log(result); } finally { await client.close(); } } example();
Python
from pymongo import MongoClient def example(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') try: db = client['test'] players = db['players'] players.insert_many([ { '_id': 1, 'game': 'A', 'player': 'Alice', 'score': 90 }, { '_id': 2, 'game': 'A', 'player': 'Bob', 'score': 85 }, { '_id': 3, 'game': 'A', 'player': 'Carol', 'score': 85 }, { '_id': 4, 'game': 'A', 'player': 'Dave', 'score': 70 }, { '_id': 5, 'game': 'B', 'player': 'Erin', 'score': 95 }, { '_id': 6, 'game': 'B', 'player': 'Frank', 'score': 80 } ]) result = list(players.aggregate([ { '$setWindowFields': { 'partitionBy': '$game', 'sortBy': { 'score': -1 }, 'output': { 'denseRank': { '$denseRank': {} } } } } ])) print(result) finally: client.close() example()