Docs Menu
Docs Home
/ / /
Ruby ドライバー
/ /

ドキュメントをカウント

このガイドでは、 Rubyドライバーを使用して、コレクション内のドキュメント数の正確で推定値を取得する方法を学習できます。次のメソッドは、コレクション内のドキュメントをカウントします。

  • count_documents:クエリフィルターに一致するドキュメント、またはコレクションに存在するドキュメントの正確な数を返します

  • estimated_document_count:コレクションに存在するドキュメントの推定数を返します

このガイドの例では、 Atlasサンプルデータセットsample_trainingデータベースの companiesコレクションを使用します。Rubyアプリケーションからこのコレクションにアクセスするには、Atlas クラスターに接続する Mongo::Clientオブジェクトを作成し、次の値を database 変数と collection 変数に割り当てます。

database = client.use('sample_training')
collection = database['companies']

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

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

Tip

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

コレクション内のすべてのドキュメントの数を返すには、次の例に示すように、クエリフィルターを渡せずに count_documents メソッドを呼び出します。

result = collection.count_documents
puts "Number of documents: #{result}"
Number of documents: 9500

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

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

result = collection.count_documents(founded_year: 2010)
puts "Number of companies founded in 2010: #{result}"
Number of companies founded in 2010: 33

オプション値を指定する 2 つ目のパラメーターを渡すことで、count_documents メソッドの動作を変更できます。次の表では、カウント操作をカスタマイズするために設定できるオプションについて説明しています。

オプション
説明

collation

The collation to use for the operation.
Type: Hash

hint

The index to use for the operation.
Type: Hash

comment

The comment to attach to the operation.
Type: Object

limit

The maximum number of documents to count. This value must be a positive integer.
Type: Integer

max_time_ms

The maximum amount of time in milliseconds that the operation can run.
Type: Integer

skip

The number of documents to skip before counting documents.
Type: Integer

read

The read preference to use for the operation. To learn more, see Read Preference in the MongoDB Server manual.
Type: Hash

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

result = collection.count_documents({ number_of_employees: 50 }, limit: 100)
puts "Number of companies with 50 employees: #{result}"
Number of companies with 50 employees: 100

重要

オプション パラメータを count_documents メソッドに渡す場合は、クエリフィルターを括弧で囲む必要があります({})。

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

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

result = collection.estimated_document_count
puts "Estimated number of documents: #{result}"
Estimated number of documents: 9500

オプション値を指定するパラメーターを渡すことで、estimated_document_count メソッドの動作を変更できます。次の表では、操作をカスタマイズするために設定できるオプションについて説明します。

オプション
説明

comment

The comment to attach to the operation.
Type: Object

max_time_ms

The maximum amount of time in milliseconds that the operation can run.
Type: Integer

read

The read concern to use for the operation. To learn more, see Read Concern in the MongoDB Server manual.
Type: Hash

次の例では、 estimated_document_countメソッドを使用してコレクション内のドキュメント数の推定値を返し、操作のタイムアウトを1000ミリ秒に設定します。

result = collection.estimated_document_count(max_time_ms: 1000)
puts "Estimated number of documents: #{result}"
Estimated number of documents: 9500

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

戻る

個別のフィールド値

項目一覧