기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
$rank
버전 8.0.2에서 새로 추가되었습니다.
Amazon DocumentDB의 $rank 창 연산자는 $setWindowFields 단계 sortBy 순서에 따라 파티션 내의 각 문서의 위치를 반환합니다. sortBy 값이 동일한 문서는 동일한 순위를 받으며, 다음 고유 값은 중간 위치를 건너뛰어 갭(예: 1, 2, 2, 4)을 남깁니다. $setWindowFields 스테이지의 output 필드 아래에 지정됩니다.
$setWindowFields 스테이지에서만 사용되는 창 연산자입니다.
파라미터
-
$rank는 빈 문서를{}값으로 사용합니다. -
window프레임은 허용되지 않습니다. -
$setWindowFields스테이지에 정확히 하나의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하므로 다음 순위는 Dave4의 로 건너뜁니다.
코드 예제
$setWindowFields 단계에서 $rank 연산자를 사용하기 위한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다.