Retrieve Data
Overview
このガイドでは、 Cドライバーを使用して、読み取り操作によりMongoDBコレクションからデータを検索する方法を学習できます。 mongoc_collection_find_with_opts()
関数を呼び出して、クエリフィルターで指定された一連の条件に一致するドキュメントを検索できます。
サンプル データ
このガイドの例では、 Atlas サンプル データセットの
sample_restaurants
データベースのrestaurants
コレクションを使用します。 MongoDB Atlas クラスターを無料で作成して、サンプル データセットをロードする方法については、 「 Atlas を使い始める 」ガイドを参照してください。
ドキュメントの検索
mongoc_collection_find_with_opts()
関数はクエリフィルターを受け取り、コレクションから一致するすべてのドキュメントを返します。クエリフィルターは、ドライバーがコレクションのドキュメントを照合するために使用する基準を指定するドキュメントです。
クエリフィルターの詳細については、「クエリの指定」ガイドを参照してください。
ドキュメントの検索の例
次の例では、 mongoc_collection_find_with_opts()
関数を使用して、cuisine
フィールドの値が "Spanish"
であるすべてのドキュメントを検索します。
bson_t *filter = BCON_NEW ("cuisine", BCON_UTF8 ("Spanish")); mongoc_cursor_t *results = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
前の例の mongoc_collection_find_with_opts()
操作はmongoc_cursor_t *
を返します。これを反復処理するには、mongoc_cursor_next()
関数と を使用します。 次の例ではを反復処理し、前のクエリで返された結果を出力します。
const bson_t *doc; bson_error_t error; while (mongoc_cursor_next (results, &doc)) { char *str = bson_as_canonical_extended_json (doc, NULL); printf ("%s\n", str); bson_free (str); } // Ensure we iterated through cursor without error if (mongoc_cursor_error (results, &error)) { fprintf (stderr, "Error getting results: %s\n", error.message); } mongoc_cursor_destroy (results); bson_destroy (filter);
{ "_id" : { "$oid" : "..." }, "name" : "Charle'S Corner Restaurant & Deli", "cuisine" : "Spanish", ... } { "_id" : { "$oid" : "..." }, "name" : "Real Madrid Restaurant", "cuisine" : "Spanish", ... } { "_id" : { "$oid" : "..." }, "name" : "Malaga Restaurant", "cuisine" : "Spanish", ... } { "_id" : { "$oid" : "..." }, "name" : "Cafe Espanol", "cuisine" : "Spanish", ... } { "_id" : { "$oid" : "..." }, "name" : "Cafe Riazor", "cuisine" : "Spanish", ... } ...
注意
すべてのドキュメントの検索
コレクション内のすべてのドキュメントを検索するには、mongoc_collection_find_with_opts()
関数に空のフィルターを渡します。
bson_t *empty_filter = bson_new (); mongoc_cursor_t *results = mongoc_collection_find_with_opts (collection, empty_filter, NULL, NULL); mongoc_cursor_destroy (results); bson_destroy (empty_filter);
検索動作の変更
構成するオプションを含む bson_t
構造を渡すことで、mongoc_collection_find_with_opts()
関数の動作を変更できます。 次の表では、クエリの変更に一般的に使用されるオプションについて説明しています。
オプション | 説明 |
---|---|
| Sets the collation options for the query. |
| Specifies a string to attach to the query. This can help you trace and interpret the
operation in the server logs and in profile data. To learn more about query comments,
see the cursor.comment() page in the
MongoDB Server manual. |
| Specifies the index to use for the query. |
| Limits the number of documents to be returned from the query. |
| Sets the maximum execution time on the server for this operation. |
| Sets the number of documents to skip. |
| Defines the sort criteria to apply to the query. |
次の例では、 limit
と maxTimeMS
オプションを使用して、クエリによって返されるドキュメント数を 10
に制限し、操作の最大実行時間を 10000
ミリ秒に設定します。
bson_t *filter = BCON_NEW ("cuisine", BCON_UTF8 ("Spanish")); bson_t *opts = BCON_NEW ("limit", BCON_INT32 (10), "maxTimeMS", BCON_INT32 (10000)); mongoc_cursor_t *results = mongoc_collection_find_with_opts (collection, filter, opts, NULL); mongoc_cursor_destroy (results); bson_destroy (filter); bson_destroy (opts);
の動作を変更するオプションの完全なリストについては、 mongoc_collection_find_with_opts()
MongoDB Serverマニュアルの find メソッドのドキュメントを参照してください。
詳細情報
クエリフィルターの詳細については、「クエリの指定」を参照してください。
Cドライバーを使用してドキュメントを取得する実行可能なコード例については、「データの読み取り」を参照してください。
API ドキュメント
このガイドで説明されている関数の詳細については、次のAPIドキュメントを参照してください。