翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
$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演算子を使用するコード例を表示するには、使用する言語のタブを選択します。