View a markdown version of this page

$DenseRank - Amazon DocumentDB

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

$DenseRank

Baru dari versi 8.0.2.

Operator $denseRank 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 menggunakan posisi segera berikut, tanpa meninggalkan celah (misalnya,1, 2, 2, 3). Ini ditentukan di bawah output bidang pang $setWindowFields gung.

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

Parameter

  • $denseRankmengambil 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 setiap pemain peringkat padat.

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

Keluaran

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

Dalam setiap game partisi, pemain diberi peringkat berdasarkan penurunanscore. Bob dan Carol bersaing 85 dan berbagi peringkat2, dan Dave mengikuti di peringkat 3 tanpa celah.

Contoh kode

Untuk melihat contoh kode untuk menggunakan $denseRank 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: { 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()