Overview
このガイドでは、 C++ドライバーを使用して、コレクション全体で指定されたフィールドの個別の値を検索する方法を学習できます。
コレクション内では、異なるドキュメントによって単一のフィールドの異なる値が含まれる場合があります。 例、 restaurantsコレクション内の 1 つのドキュメントのborough値は"Manhattan"で、別のドキュメントのborough値は"Queens"です。 C++ドライバーを使用すると、コレクション内の複数のドキュメントにわたってフィールドに含まれるすべての一意の値を検索できます。
サンプル データ
このガイドの例では、 Atlasサンプルデータセットのsample_restaurantsデータベースのrestaurantsコレクションを使用します。 C++アプリケーションからこのコレクションにアクセスするには、Atlasmongocxx::client クラスターに接続する をインスタンス化し、 変数と 変数に次の値を割り当てます。dbcollection
auto db = client["sample_restaurants"]; auto collection = db["restaurants"];
MongoDB Atlasクラスターを無料で作成して、サンプルデータセットをロードする方法については、 「Atlas を使い始める」ガイドを参照してください。
distinct() 方式
指定したフィールドの個別の値を検索するには、 distinct()メソッドを呼び出し、個別の値を検索するフィールドの名前を渡します。
コレクション全体で個別の値を取得
次の例では、 restaurantsコレクション内のboroughフィールドの個別の値を取得します。
auto cursor = collection.distinct("borough", {}); for(auto&& doc : cursor) { std::cout << bsoncxx::to_json(doc) << std::endl; }
{ "values" : [ "Bronx", "Brooklyn", "Manhattan", "Missing", "Queens", "Staten Island" ], "ok" : 1.0, "$clusterTime" : { "clusterTime" : { "$timestamp" : { ... } }, "signature" : { "hash" : { ... }, "keyId" : ... } }, "operationTime" : { "$timestamp" : { ... } }
この操作では、1 つのドキュメントを含むカーソルが返されます。 ドキュメントには、個別のboroughフィールド値と操作に関するメタデータのリストが含まれています。 boroughフィールドでは複数のドキュメントが同じ値になっていますが、各値は結果に 1 回だけ表示されます。
指定されたドキュメント全体で個別の値を取得
distinct()メソッドにクエリフィルターを提供すると、コレクション内のドキュメントのサブセット全体で個別のフィールド値を検索できます。 クエリフィルターは、操作内のドキュメントを一致させるために使用される検索条件を指定する式です。 クエリフィルターの作成の詳細については、 クエリの指定ガイドを参照してください。
次の例では、 cuisineフィールドの値が"Italian"であるすべてのドキュメントのboroughフィールドの個別の値を取得します。
auto cursor = collection.distinct("borough", make_document(kvp("cuisine", "Italian"))); for(auto&& doc : cursor) { std::cout << bsoncxx::to_json(doc) << std::endl; }
{ "values" : [ "Bronx", "Brooklyn", "Manhattan", "Queens", "Staten Island" ], "ok" : 1.0, "$clusterTime" : { "clusterTime" : { "$timestamp" : { ... }, "signature" : { "hash" : { ... }, "keyId" : ... } }, "operationTime" : { "$timestamp" : { ... } }
個別の動作の変更
mongocxx::options::distinctクラスのインスタンスを引数として渡すことで、 distinct()メソッドの動作を変更できます。 次の表では、 mongocxx::options::distinctインスタンスで設定できるフィールドを説明しています。
フィールド | 説明 |
|---|---|
| The collation to use for the operation. Type: bsoncxx::document::view_or_value |
| The maximum amount of time in milliseconds that the operation can run. Type: std::chrono::milliseconds |
| The comment to attach to the operation. Type: bsoncxx::types::bson_value::view_or_value |
| The read preference to use for the operation. Type: mongocxx::read_preference |
次の例では、 boroughフィールド値が"Bronx"で、かつcuisineフィールド値が"Pizza"であるすべてのドキュメントのnameフィールドの個別の値を取得します。 また、オプションインスタンスのcommentフィールドを指定して、操作にコメントを追加します。
mongocxx::options::distinct opts{}; opts.comment(bsoncxx::types::bson_value::view_or_value{"Bronx pizza restaurants"}); auto cursor = collection.distinct("name", make_document(kvp("$and", make_array(make_document(kvp("borough", "Bronx")), make_document(kvp("cuisine", "Pizza"))))), opts ); for (auto&& doc : cursor) { std::cout << bsoncxx::to_json(doc) << std::endl; }
{ "values" : [ "$1.25 Pizza", "18 East Gunhill Pizza", "2 Bros", "Aenos Pizza", "Alitalia Pizza Restaurant", … ], "ok" : 1.0, "$clusterTime" : { "clusterTime" : { … }, "signature" : { … }, "keyId" : … } }, "operationTime" : { … } }
API ドキュメント
このガイドで説明したメソッドや型の詳細については、次の API ドキュメントを参照してください。