Overview
このガイドでは、 Cドライバーを使用して、コレクション全体で指定されたフィールドの個別の値を検索する方法を学習できます。
コレクション内では、異なるドキュメントによって単一のフィールドの異なる値が含まれる場合があります。 例、restaurantコレクション内の 1 つのドキュメントの borough 値は "Manhattan" で、別のドキュメントの borough 値は "Queens" です。 Cドライバーを使用すると、コレクション内の複数のドキュメントにわたってフィールドに含まれるすべての個別の値を検索できます。
サンプル データ
このガイドの例では、restaurants sample_restaurantsAtlasサンプルデータセットの データベースの コレクションを使用します。 MongoDB Atlasクラスターを無料で作成して、サンプルデータセットをロードする方法については、 MongoDBを使い始めるガイドを参照してください 。
個別のコマンド
指定されたフィールドの個別の値を取得するには、mongoc_collection_read_command_with_opts() 関数を呼び出し、その関数に distinct コマンドを使用するように指示します。 また、データを取得するコレクションとフィールドを指定する必要があります。
コレクション全体で個別の値を取得
次の例では、 restaurantsコレクション内のboroughフィールドの個別の値を取得します。
bson_t reply; bson_error_t error; bson_t *command = BCON_NEW("distinct", BCON_UTF8("restaurants"), "key", BCON_UTF8("borough")); if (!mongoc_collection_read_command_with_opts(collection, command, NULL, NULL, &reply, &error)) { fprintf(stderr, "An error occurred: %s\n", error.message); } else { char *str = bson_as_canonical_extended_json(&reply, NULL); printf("%s\n", str); bson_free(str); } bson_destroy(&reply); bson_destroy(command);
{ "values" : [ "Bronx", "Brooklyn", "Manhattan", "Missing", "Queens", "Staten Island" ], ... }
結果には、コレクション内のすべてのドキュメントにわたってboroughフィールドに表示されるすべての個別の値が表示されます。 boroughフィールドの値は複数のドキュメントで同じですが、各値は結果に 1 回だけ表示されます。
指定されたドキュメント全体で個別の値を取得
distinctコマンドにクエリフィルターを指定すると、コレクション内のドキュメントのサブセット全体で個別のフィールド値を検索できます。クエリフィルターは、操作内のドキュメントを一致させるために使用される検索条件を指定する式です。
クエリフィルターの作成の詳細については、「 クエリの指定 」を参照してください。
次の例では、 cuisineフィールドの値が"Italian"であるすべてのドキュメントのboroughフィールドの個別の値を取得します。
bson_t reply; bson_error_t error; bson_t *query = BCON_NEW("cuisine", BCON_UTF8("Italian")); bson_t *command = BCON_NEW("distinct", BCON_UTF8("restaurants"), "key", BCON_UTF8("borough"), "query", BCON_DOCUMENT(query)); if (!mongoc_collection_read_command_with_opts(collection, command, NULL, NULL, &reply, &error)) { fprintf(stderr, "An error occurred: %s\n", error.message); } else { char *str = bson_as_canonical_extended_json(&reply, NULL); printf("%s\n", str); bson_free(str); } bson_destroy(&reply); bson_destroy(command); bson_destroy(query);
{ "values" : [ "Bronx", "Brooklyn", "Manhattan", "Queens", "Staten Island" ], ... }
個別の動作の変更
distinct コマンドは、mongoc_collection_read_command_with_opts() 関数にオプションを渡すことで変更できます。 オプションを指定しない場合、ドライバーは操作をカスタマイズしません 。
次の表では、 distinct操作 をカスタマイズするために使用できるいくつかのオプションについて説明しています。
オプション | 説明 |
|---|---|
| Specifies the kind of language collation to use when sorting
results. For more information, see Collation
in the MongoDB Server manual. |
| Specifies a comment to attach to the operation. |
操作 を変更するために使用できるオプションの完全なリストについては、 MongoDB Serverマニュアルの 個別のdistinct ドキュメントを参照してください。
次の例では、 boroughフィールド値が"Bronx"で、かつcuisineフィールド値が"Pizza"であるすべてのドキュメントのnameフィールドの個別の値を取得します。 また、 commentオプションを使用して操作にコメントを追加します。
bson_t reply; bson_error_t error; bson_t *query = BCON_NEW("borough", BCON_UTF8("Bronx"), "cuisine", BCON_UTF8("Pizza")); bson_t *command = BCON_NEW("distinct", BCON_UTF8("restaurants"), "key", BCON_UTF8("name"), "query", BCON_DOCUMENT(query)); bson_t *opts = BCON_NEW("comment", BCON_UTF8("Bronx pizza restaurants")); if (!mongoc_collection_read_command_with_opts(collection, command, NULL, opts, &reply, &error)) { fprintf(stderr, "An error occurred: %s\n", error.message); } else { char *str = bson_as_canonical_extended_json(&reply, NULL); printf("%s\n", str); bson_free(str); } bson_destroy(&reply); bson_destroy(command); bson_destroy(query); bson_destroy(opts);
{ "values" : [ "$1.25 Pizza", "18 East Gunhill Pizza", "2 Bros", "Aenos Pizza", "Alitalia Pizza Restaurant", ... ], ... }
詳細情報
個別の コマンドの詳細については、 MongoDB Serverマニュアルの 個別の ページを参照してください。
API ドキュメント
mongoc_collection_read_command_with_opts() 関数の詳細については、APIドキュメントを参照してください。