$skip - Amazon DocumentDB

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

$skip

Di Amazon DocumentDB, $skip operator digunakan untuk mengimbangi titik awal hasil kueri, memungkinkan Anda untuk mengambil subset tertentu dari dokumen yang cocok. Ini sangat berguna dalam skenario pagination, di mana Anda ingin mengambil halaman hasil berikutnya.

Parameter

  • skip: Jumlah dokumen yang harus dilewati sebelum mengembalikan dokumen yang tersisa.

Contoh (MongoDB Shell)

Contoh berikut menunjukkan bagaimana menggunakan $skip operator untuk mengambil halaman kedua hasil (dokumen 11-20) dari koleksi.

Buat dokumen sampel

db.collection.insert([ { "name": "Document 1" }, { "name": "Document 2" }, { "name": "Document 3" }, { "name": "Document 4" }, { "name": "Document 5" }, { "name": "Document 6" }, { "name": "Document 7" }, { "name": "Document 8" }, { "name": "Document 9" }, { "name": "Document 10" }, { "name": "Document 11" }, { "name": "Document 12" }, { "name": "Document 13" }, { "name": "Document 14" }, { "name": "Document 15" }, { "name": "Document 16" }, { "name": "Document 17" }, { "name": "Document 18" }, { "name": "Document 19" }, { "name": "Document 20" } ]);

Contoh kueri

db.collection.find({}, { "name": 1 }) .skip(10) .limit(10);

Keluaran

[ { "_id" : ObjectId("..."), "name" : "Document 11" }, { "_id" : ObjectId("..."), "name" : "Document 12" }, { "_id" : ObjectId("..."), "name" : "Document 13" }, { "_id" : ObjectId("..."), "name" : "Document 14" }, { "_id" : ObjectId("..."), "name" : "Document 15" }, { "_id" : ObjectId("..."), "name" : "Document 16" }, { "_id" : ObjectId("..."), "name" : "Document 17" }, { "_id" : ObjectId("..."), "name" : "Document 18" }, { "_id" : ObjectId("..."), "name" : "Document 19" }, { "_id" : ObjectId("..."), "name" : "Document 20" } ]

Contoh kode

Untuk melihat contoh kode untuk menggunakan $skip perintah, pilih tab untuk bahasa yang ingin Anda gunakan:

Node.js
const { MongoClient } = require('mongodb'); async function main() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const collection = db.collection('collection'); const results = await collection.find({}, { projection: { name: 1 } }) .skip(10) .limit(10) .toArray(); console.log(results); await client.close(); } main();
Python
from pymongo import MongoClient def main(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client.mydatabase collection = db.collection results = list(collection.find({}, {'name': 1}) .skip(10) .limit(10)) print(results) client.close() if __name__ == '__main__': main()