AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

個別のフィールド値の取得

このガイドでは、 Cドライバーを使用して、コレクション全体で指定されたフィールドの個別の値を検索する方法を学習できます。

コレクション内では、異なるドキュメントによって単一のフィールドの異なる値が含まれる場合があります。例、restaurantコレクション内の 1 つのドキュメントの borough 値は "Manhattan" で、別のドキュメントの borough 値は "Queens" です。Cドライバーを使用すると、コレクション内の複数のドキュメントにわたってフィールドに含まれるすべての個別の値を検索できます。

このガイドの例では、Atlas サンプルデータセットsample_restaurants データベースの restaurants コレクションを使用します。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);

結果には、コレクション内のすべてのドキュメントにわたって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);

distinct コマンドは、mongoc_collection_read_command_with_opts() 関数にオプションを渡すことで変更できます。 オプションを指定しない場合、ドライバーは操作をカスタマイズしません 。

次の表では、 distinct操作 をカスタマイズするために使用できるいくつかのオプションについて説明しています。

オプション
説明

collation

Specifies the kind of language collation to use when sorting results. For more information, see Collation in the MongoDB Server manual.

comment

操作に添付するコメントを指定します。

操作 を変更するために使用できるオプションの完全なリストについては、 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);

個別の コマンドの詳細については、 MongoDB Serverマニュアルの 個別の ページを参照してください。

mongoc_collection_read_command_with_opts() 関数の詳細については、APIドキュメントを参照してください。