Overview
このガイドでは、 Rustドライバーを使用して、コレクション内のドキュメント数の正確な推定値を取得する方法を説明します。 count_documents() メソッドは、クエリフィルターに一致するクエリフィルター、またはコレクションに存在するドキュメントの正確な数を返し、estimated_document_count() メソッドはコレクション内のドキュメントの推定数を返します。
サンプル データ
このガイドの例では、restaurants sample_restaurantsAtlasサンプルデータセットの データベースの コレクションを使用します。 Rustアプリケーションからこのコレクションにアクセスするには、AtlasClient クラスターに接続する を作成し、次の値をmy_coll 変数に割り当てます。
AsynchronousSynchronous各実行時に対応するコードを表示するには、 タブまたは タブを選択します。
let uri = "<connection string>"; let client = Client::with_uri_str(uri).await?; let my_coll: Collection<Document> = client .database("sample_restaurants") .collection("restaurants");
let uri = "<connection string>"; let client = Client::with_uri_str(uri)?; let my_coll: Collection<Document> = client .database("sample_restaurants") .collection("restaurants");
MongoDB Atlasクラスターを無料で作成し、サンプルデータセットをロードする方法については、 「 Rustドライバーを使い始める 」ガイドを参照してください。
このガイドの例では、 restaurantsコレクション内のドキュメントのモデルとして次のRestaurant構造体を使用します。
struct Restaurant { name: String, cuisine: String, }
正確なカウントの取得
コレクション内のドキュメントの数をカウントするには、count_documents() メソッドを使用します。特定の検索条件に一致するドキュメントの数をカウントするには、クエリフィルタードキュメントをcount_documents() メソッドに渡します。
クエリの指定の詳細については、「 クエリの指定 」ガイドを参照してください。
すべてのドキュメントをカウントする
コレクション内のすべてのドキュメントの数を返すには、次の例に示すように、空の フィルタードキュメント をcount_documents() メソッドに渡します。各実行時に対応するコードを表示するには、Asynchronous タブまたは Synchronousタブを選択します。
let ct = my_coll.count_documents().await?; println!("Number of matching documents: {}", ct);
Number of documents: 25216
let ct = my_coll.count_documents().run()?; println!("Number of matching documents: {}", ct);
Number of documents: 25216
特定のドキュメントのカウント
特定の検索条件に一致するドキュメントの数を返すには、クエリフィルタードキュメントをcount_documents()メソッドに渡します。
次の例では、nameフィールドの値に string "Sunset" が含まれるドキュメントの数をカウントします。各実行時に対応するコードを表示するには、Asynchronous タブまたは Synchronousタブを選択します。
let ct = my_coll .count_documents(doc! { "name": doc! { "$regex": "Sunset" } }) .await?; println!("Number of matching documents: {}", ct);
Number of matching documents: 10
let ct = my_coll .count_documents(doc! { "name": doc! { "$regex": "Sunset" } }) .run()?; println!("Number of matching documents: {}", ct);
Number of matching documents: 10
カウント動作をカスタマイズする
count_documents() メソッドの動作を変更するには、await または run() メソッドを呼び出す前に、オプション メソッドを count_documents() 呼び出しに連結します。次の表では、カウント操作をカスタマイズするために設定できるオプションについて説明しています。
オプション | 説明 |
|---|---|
| The collation to use for the operation. Type: Collation |
| The index to use for the operation. Type: Hint |
| The comment to attach to the operation. Type: Bson |
| The maximum number of documents to count. This value must be a positive integer. Type: u64 |
| The maximum amount of time in milliseconds that the operation can run. Type: Duration |
| The read concern to use for the operation. Type: ReadConcern |
| The number of documents to skip before counting documents. Type: u64 |
| The read preference and tags to use for the operation. Type: SelectionCriteria |
推定カウントの取得
await メソッドまたは run() メソッドを呼び出す前に estimated_document_count() メソッドを呼び出すことで、コレクション内のドキュメント数の推定値を取得できます。メソッドは、コレクションメタデータに基づいてドキュメントの量を推定します。これは、正確なカウントを実行するよりも高速です。
次の例では、コレクション内のドキュメントの数を見積ります。各実行時に対応するコードを表示するには、Asynchronous タブまたは Synchronousタブを選択します。
let ct = my_coll.estimated_document_count().await?; println!("Number of documents: {}", ct);
Number of documents: 25216
let ct = my_coll.estimated_document_count().run()?; println!("Number of documents: {}", ct);
Number of documents: 25216
推定カウント動作をカスタマイズ
オプション メソッドを estimated_document_count() 呼び出しに連結することで、estimated_document_count() メソッドの動作を変更できます。次の表では、推定カウント操作 をカスタマイズするために設定できるオプションについて説明します。
オプション | 説明 |
|---|---|
| The maximum amount of time in milliseconds that the operation can run. Type: Duration |
| The comment to attach to the operation. Type: Bson |
| The read concern to use for the operation. Type: ReadConcern |
| The read preference and tags to use for the operation. Type: SelectionCriteria |
API ドキュメント
このガイドで説明したメソッドや型の詳細については、次の API ドキュメントを参照してください。