Docs Menu
Docs Home
/ / /
C++ ドライバー
/

ドキュメントをカウント

項目一覧

  • Overview
  • サンプル データ
  • 正確なカウントの取得
  • すべてのドキュメントをカウントする
  • 特定のドキュメントのカウント
  • カウント動作をカスタマイズする
  • 推定カウントの取得
  • 推定カウント動作をカスタマイズ
  • API ドキュメント

このガイドでは、 C++ドライバーを使用して、コレクション内のドキュメント数の正確な推定値を取得する方法を学習できます。 count_documents()メソッドは、クエリフィルターに一致するドキュメント、またはコレクションに存在するドキュメントの正確な数を返し、 estimated_document_count()メソッドはコレクション内のドキュメントの推定数を返します。

このガイドの例では、 Atlasサンプルデータセットsample_trainingデータベースのcompaniesコレクションを使用します。 C++アプリケーションからこのコレクションにアクセスするには、Atlasmongocxx::client クラスターに接続する をインスタンス化し、 変数と 変数に次の値を割り当てます。dbcollection

auto db = client["sample_training"];
auto collection = db["companies"];

MongoDB Atlasクラスターを無料で作成して、サンプルデータセットをロードする方法については、 「Atlas を使い始める」ガイドを参照してください。

コレクション内のドキュメントの数をカウントするには、 count_documents()メソッドを使用します。 特定の検索条件に一致するドキュメントの数をカウントするには、クエリフィルタードキュメントをcount_documents()メソッドに渡します。

クエリの指定の詳細については、「 クエリの指定」ガイドを参照してください。

コレクション内のすべてのドキュメントの数を返すには、次の例に示すように、空の フィルタードキュメントをcount_documents()メソッドに渡します。

auto result = collection.count_documents({});
std::cout << "Number of documents: " << result << std::endl;
Number of documents: 9500

特定の検索条件に一致するドキュメントの数を返すには、クエリフィルタードキュメントをcount_documents()メソッドに渡します。

次の例では、 founded_yearの値が2010であるドキュメントの数をカウントします。

auto result = collection.count_documents(make_document(kvp("founded_year", 2010)));
std::cout << "Number of companies founded in 2010: " << result << std::endl;
Number of companies founded in 2010: 33

mongocxx::options::countクラスのインスタンスをパラメーターとして渡すことで、 count_documents()メソッドの動作を変更できます。 次の表では、 mongocxx::options::countインスタンスで設定できるフィールドを説明しています。

フィールド
説明
collation
The collation to use for the operation.
Type: bsoncxx::document::view_or_value
hint
The index to use for the operation.
Type: mongocxx::hint
comment
The comment to attach to the operation.
Type: bsoncxx::types::bson_value::view_or_value
limit
The maximum number of documents to count. This value must be a positive integer.
Type: std::int64_t
max_time
The maximum amount of time in milliseconds that the operation can run.
Type: std::chrono::milliseconds
skip
The number of documents to skip before counting documents.
Type: std::int64_t
read_preference
The read preference to use for the operation.
Type: mongocxx::read_preference

次の例では、 count_documents()メソッドを使用して、 number_of_employeesフィールドの値が50であるドキュメントの数をカウントし、最大100の結果をカウントするように操作に指示します。

mongocxx::options::count opts;
opts.limit(100);
auto result = collection.count_documents(make_document(kvp("number_of_employees", 50)), opts);
std::cout << "Number of companies with 50 employees: " << result << std::endl;
Number of companies with 50 employees: 100

estimated_document_count()メソッドを呼び出すと、コレクション内のドキュメント数の推定値を取得できます。 メソッドは、コレクションメタデータに基づいてドキュメントの量を推定します。これは、正確なカウントを実行するよりも高速である可能性があります。

次の例では、 コレクション内のドキュメントの数を見積ります。

auto result = collection.estimated_document_count();
std::cout << "Estimated number of documents: " << result << std::endl;
Estimated number of documents: 9500

mongocxx::options::estimated_document_countクラスのインスタンスをパラメーターとして渡すことで、 estimated_document_count()メソッドの動作を変更できます。 次の表では、 mongocxx::options::estimated_document_countインスタンスで設定できるフィールドを説明しています。

フィールド
説明
max_time
The maximum amount of time in milliseconds that the operation can run.
Type: std::chrono::milliseconds
comment
The comment to attach to the operation.
Type: bsoncxx::types::bson_value::view_or_value
read_preference
The read preference to use for the operation.
Type: mongocxx::read_preference

次の例では、 estimated_document_count()メソッドを使用してコレクション内のドキュメント数の推定値を返し、最大1000ミリ秒にわたって操作を実行するように指示します。

mongocxx::options::estimated_document_count opts;
opts.max_time(std::chrono::milliseconds{1000});
auto result = collection.estimated_document_count(opts);
std::cout << "Estimated number of documents: " << result << std::endl;
Estimated number of documents: 9500

このガイドで説明したメソッドや型の詳細については、次の API ドキュメントを参照してください。

戻る

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