View a markdown version of this page

$Nomor dokumen - Amazon DocumentDB

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

$Nomor dokumen

Baru dari versi 8.0.2.

Operator $documentNumber jendela di Amazon DocumentDB mengembalikan nomor berurutan yang berbeda untuk setiap dokumen di partisinya, sesuai dengan sortBy urutan $setWindowFields tahap. Tidak seperti $rank dan$denseRank, itu tidak memperlakukan ikatan secara khusus: setiap dokumen menerima nomor unik (misalnya,1, 2, 3, 4), dan dokumen dengan sortBy nilai yang sama diberi nomor sesuai urutan yang ditemui. Ini ditentukan di bawah output bidang pang $setWindowFields gung.

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

Parameter

  • $documentNumbermengambil 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 dengan score urutan menurun, dan memberikan setiap pemain nomor dokumen berurutan.

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

Keluaran

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

Dalam setiap game partisi, setiap pemain menerima nomor berurutan yang berbeda dalam score urutan menurun. Bob dan Carol ber 85 pasangan tetapi masih diberi nomor 2 dan 3 dalam urutan mereka ditemui.

Contoh kode

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