View a markdown version of this page

$documentNumber - Amazon DocumentDB

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

$documentNumber

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

Amazon DocumentDB의 $documentNumber 창 연산자는 $setWindowFields 단계 sortBy 순서에 따라 파티션의 각 문서에 대해 고유한 순차 번호를 반환합니다. $rank 및와 달리$denseRank, 모든 문서는 고유한 번호(예: 1, 2, 3, 4)를 수신하고 sortBy 값이 동일한 문서는 발생한 순서대로 번호가 매겨지므로 특별히 매듭을 취급하지 않습니다. $setWindowFields 스테이지의 output 필드 아래에 지정됩니다.

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

파라미터

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

  • 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: { documentNumber: { $documentNumber: {} } } } } ]);

출력

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

각 game 파티션 내에서 각 플레이어는 고유한 순차적 번호를 내림차score순으로 받습니다. Bob과 Carol은에 묶여 85 있지만 여전히 번호가 매겨2져 있고 발생한 3 순서대로 묶여 있습니다.

코드 예제

$setWindowFields 단계에서 $documentNumber 연산자를 사용하기 위한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다.

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