本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
$denseRank
8.0.2 版的新功能。
Amazon DocumentDB 中的 $denseRank window Operator 會根據$setWindowFields階段sortBy順序,傳回其分割區中每個文件的位置。具有相同sortBy值的文件會收到相同的排名,而下一個不同的值使用緊接著的位置,不留下任何差距 (例如 1, 2, 2, 3)。它在 $setWindowFields 階段的 output 欄位下指定。
這是僅在$setWindowFields階段中使用的視窗運算子。
參數
範例 (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。Bob 和 Carol 在 綁定85並共用排名 2,Dave 在排名中跟隨 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()