$skip - Amazon DocumentDB

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

$skip

在 Amazon DocumentDB 中,$skip运算符用于偏移查询结果的起点,允许您检索匹配文档的特定子集。这在分页场景中特别有用,在这种场景中,您要检索结果的后续页面。

参数

  • skip:返回剩余文档之前要跳过的文档数。

示例(MongoDB 外壳)

以下示例演示如何使用$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()