View a markdown version of this page

$ peringkat - Amazon DocumentDB

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

$ peringkat

Baru dari versi 8.0.2.

Operator $rank jendela di Amazon DocumentDB mengembalikan posisi setiap dokumen dalam partisinya, sesuai dengan urutan pang $setWindowFields gungsortBy. Dokumen dengan sortBy nilai yang sama menerima peringkat yang sama, dan nilai berbeda berikutnya melewatkan posisi intervensi, meninggalkan celah (misalnya,1, 2, 2, 4). Ini ditentukan di bawah output bidang pang $setWindowFields gung.

Ini adalah operator jendela yang hanya digunakan di pang $setWindowFields gung.

Parameter

  • $rankmengambil dokumen kosong {} sebagai nilainya.

  • Itu tidak menerima window bingkai.

  • Ini membutuhkan tepat satu sortBy kunci di pang $setWindowFields gung.

Contoh (MongoDB Shell)

Contoh berikut mempartisi dokumen berdasarkangame, mengurutkan setiap partisi score dalam urutan menurun, dan memberikan peringkat setiap pemain.

Buat dokumen sampel

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 } ]);

Contoh kueri

db.players.aggregate([ { $setWindowFields: { partitionBy: "$game", sortBy: { score: -1 }, output: { rank: { $rank: {} } } } } ]);

Keluaran

[ { "_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 } ]

Dalam setiap game partisi, pemain diberi peringkat berdasarkan penurunanscore. Bob dan Carol 85 bersaing dan berbagi peringkat2, jadi peringkat berikutnya beralih ke 4 Dave.

Contoh kode

Untuk melihat contoh kode untuk menggunakan $rank operator di pang $setWindowFields gung, pilih tab untuk bahasa yang ingin Anda gunakan:

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