$skip - Amazon DocumentDB

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

$skip

在 Amazon DocumentDB 中,運算$skip子可用來偏移查詢結果的起點,讓您擷取相符文件的特定子集。這在您要擷取後續結果頁面的分頁案例中特別有用。

參數

  • skip:傳回其餘文件之前要略過的文件數量。

範例 (MongoDB Shell)

下列範例示範如何使用 $skip運算子從集合擷取第二個結果頁面 (文件 11-20)。

建立範例文件

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

查詢範例

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

輸出

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

程式碼範例

若要檢視使用 $skip命令的程式碼範例,請選擇您要使用的語言標籤:

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()