View a markdown version of this page

$rank - Amazon DocumentDB

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

$rank

バージョン 8.0.2 の新機能。

Amazon DocumentDB の$rankウィンドウ演算子は、$setWindowFieldsステージのsortBy順序に従って、パーティション内の各ドキュメントの位置を返します。同じsortBy値を持つドキュメントは同じランクを受け取り、次の個別の値は介入位置をスキップしてギャップを残します (例: 1, 2, 2, 4)。$setWindowFields ステージの outputフィールドで指定します。

これは、$setWindowFieldsステージでのみ使用されるウィンドウ演算子です。

パラメータ

  • $rank は空のドキュメントを値{}として受け取ります。

  • window フレームは受け付けません。

  • $setWindowFields ステージには 1 つのsortByキーのみが必要です。

例 (MongoDB シェル)

次の例では、ドキュメントを でパーティション化し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: { rank: { $rank: {} } } } } ]);

出力

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

各gameパーティション内で、プレイヤーは降順でランク付けされますscore。Bob と Carol は で結合85し、ランク を共有するため2、次のランクは Dave 4の にスキップされます。

コードの例

$setWindowFields ステージで $rank演算子を使用するコード例を表示するには、使用する言語のタブを選択します。

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: { rank: { $rank: {} } } } } ]).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': { 'rank': { '$rank': {} } } } } ])) print(result) finally: client.close() example()