$skip - Amazon DocumentDB

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

$skip

Amazon DocumentDB では、 $skip演算子を使用してクエリ結果の開始ポイントをオフセットし、一致するドキュメントの特定のサブセットを取得できます。これは、後続の結果ページを取得するページ分割シナリオで特に便利です。

パラメータ

  • skip: 残りのドキュメントを返す前にスキップするドキュメントの数。

例 (MongoDB シェル)

次の例は、 $skip演算子を使用してコレクションから結果の 2 ページ目 (ドキュメント 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()