翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
$denseRank
バージョン 8.0.2 の新機能。
Amazon DocumentDB の$denseRankウィンドウ演算子は、$setWindowFieldsステージのsortBy順序に従って、パーティション内の各ドキュメントの位置を返します。同じsortBy値を持つドキュメントは同じランクを受け取り、次の個別の値は直後の位置を使用してギャップを残しません (例: 1, 2, 2, 3)。$setWindowFields ステージの outputフィールドで指定します。
これは、$setWindowFieldsステージでのみ使用されるウィンドウ演算子です。
パラメータ
-
$denseRankは空のドキュメントを値{}として受け取ります。 -
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: { 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演算子を使用するコード例を表示するには、使用する言語のタブを選択します。