View a markdown version of this page

$denseRank - Amazon DocumentDB

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

$denseRank

버전 8.0.2에서 새로 추가되었습니다.

Amazon DocumentDB의 $denseRank 창 연산자는 $setWindowFields 단계 sortBy 순서에 따라 파티션 내의 각 문서의 위치를 반환합니다. sortBy 값이 동일한 문서는 동일한 순위를 받으며, 다음 고유 값은 바로 다음 위치를 사용하여 공백을 남기지 않습니다(예: 1, 2, 2, 3). $setWindowFields 스테이지의 output 필드 아래에 지정됩니다.

$setWindowFields 스테이지에서만 사용되는 창 연산자입니다.

파라미터

  • $denseRank는 빈 문서를 {} 값으로 사용합니다.

  • 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: { 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()