Overview
このガイドでは、次の操作を使用して、読み取り操作から返されるドキュメントを指定する方法を学習できます。
limit: クエリから返されるドキュメントの最大数を指定しますsort: 返されるドキュメントのソート順序を指定しますskip: クエリ結果を返す前にスキップするドキュメントの数を指定します
サンプル データ
このガイドの例では、restaurants sample_restaurantsAtlasサンプルデータセットの データベースの コレクションを使用します。 MongoDB Atlasクラスターを無料で作成して、サンプルデータセットをロードする方法については、 MongoDBを使い始めるガイドを参照してください 。
Limit
読み取り操作から返されるドキュメントの最大数を指定するには、mongoc_collection_find_with_opts() 関数呼び出しに limit オプションを渡します。
次の例では、 cuisineフィールドの値が"Italian"であるすべてのレストランを検索し、結果を5ドキュメントに制限します。
const bson_t *doc; bson_t *filter = BCON_NEW("cuisine", BCON_UTF8("Italian")); bson_t *opts = BCON_NEW("limit", BCON_INT64(5)); mongoc_cursor_t *results = mongoc_collection_find_with_opts(collection, filter, opts, NULL); while (mongoc_cursor_next(results, &doc)) { char *str = bson_as_canonical_extended_json(doc, NULL); printf("%s\n", str); bson_free(str); } bson_destroy(filter); bson_destroy(opts); mongoc_cursor_destroy(results);
{ "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "V & T Restaurant", ... } { "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Mimis Restaurant & Bar", ... } { "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Venice Restaurant", ... } { "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Areo Restaurant", ... } { "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Tre Giovani Pizza & Pasta", ... }
Tip
上記の例では、クエリによって返された最初の 5 つのドキュメントが自然な順序で返されます。 次のセクションでは、指定されたソート順序でドキュメントを返す方法について説明します。
Sort
指定した順序でドキュメントを返すには、sort オプションを使用します。 sort オプションは、並べ替え方向をパラメーターとして受け取ります。 並べ替え方向を指定するには、昇順並べ替えの場合は 1 を渡し、降順並べ替えの場合は -1 を渡します。 昇順では、注文値が最低から最高の順にソートされ、降順では、注文値が最高から最低の順にソートされます。
次の例では、cuisine フィールドの値が"Italian" であるすべてのドキュメントを、name フィールドの値で昇順にソートして返します。
const bson_t *doc; bson_t *filter = BCON_NEW("cuisine", BCON_UTF8("Italian")); bson_t *opts = BCON_NEW("sort", "{", "name", BCON_INT32(1), "}"); mongoc_cursor_t *results = mongoc_collection_find_with_opts(collection, filter, opts, NULL); while (mongoc_cursor_next(results, &doc)) { char *str = bson_as_canonical_extended_json(doc, NULL); printf("%s\n", str); bson_free(str); } bson_destroy(filter); bson_destroy(opts); mongoc_cursor_destroy(results);
{ "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "(Lewis Drug Store) Locanda Vini E Olii", ... } { "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "101 Restaurant And Bar", ... } { "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "44 Sw Ristorante & Bar", ... } { "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "900 Park", ... } { "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "A Voce", ... } ...
スキップ
クエリ結果を返す前に指定した数のドキュメントをスキップするには、skip オプションを使用して、スキップするドキュメント数を渡します。 skip オプションは、クエリ結果内の指定された数のドキュメントを無視し、残りを返します。
次の例では、 cuisineフィールドの値が"Italian"であるすべてのドキュメントを返し、最初の10ドキュメントをスキップします。
const bson_t *doc; bson_t *filter = BCON_NEW("cuisine", BCON_UTF8("Italian")); bson_t *opts = BCON_NEW("skip", BCON_INT64(10)); mongoc_cursor_t *results = mongoc_collection_find_with_opts(collection, filter, opts, NULL); while (mongoc_cursor_next(results, &doc)) { char *str = bson_as_canonical_extended_json(doc, NULL); printf("%s\n", str); bson_free(str); } bson_destroy(filter); bson_destroy(opts); mongoc_cursor_destroy(results);
{ "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Trattoria Alba", ... } { "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Da Umberto Restaurant", ... } { "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "La Strada Restaurant", ... } { "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Pasta Lovers Trattoria", ... } { "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Nanni Restaurant", ... } ...
制限、ソート、スキップの組み合わせ
1 回の操作で limit、sort、skip オプションを組み合わせることができます。 これにより、返されるソートされたドキュメントの最大数を設定して、返される前に指定された数のドキュメントをスキップできます。
次の例では、 cuisineの フィールド値が"Italian"であるドキュメントを返します。 結果はアルファベット順にソートされ、最初の10ドキュメントをスキップし、結果を5ドキュメントに限定します。
const bson_t *doc; bson_t *filter = BCON_NEW("cuisine", BCON_UTF8("Italian")); bson_t *opts = BCON_NEW("limit", BCON_INT64(5), "skip", BCON_INT64(10), "sort", "{", "name", BCON_INT32(1), "}"); mongoc_cursor_t *results = mongoc_collection_find_with_opts(collection, filter, opts, NULL); while (mongoc_cursor_next(results, &doc)) { char *str = bson_as_canonical_extended_json(doc, NULL); printf("%s\n", str); bson_free(str); } bson_destroy(filter); bson_destroy(opts); mongoc_cursor_destroy(results);
{ "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Acqua", ... } { "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Acqua Restaurant", ... } { "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Acqua Santa", ... } { "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Acquista Trattoria", ... } { "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Acquolina Catering", ... }
注意
これらのメソッドを呼び出す順序によって、返されるドキュメントは変更されません。 ドライバーは、ソートとスキップ操作を最初に実行し、その後に制限操作を実行するために、呼び出しの順序を自動的に並べ替えます。
詳細情報
クエリの指定の詳細については、 「 クエリの指定 」を参照してください。
ドキュメント検索の詳細については、ドキュメントの検索を参照してください。
API ドキュメント
mongoc_collection_find_with_opts() 関数の詳細については、APIドキュメントを参照してください。