Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /

ドキュメントをカウント

このガイドでは、 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構造体を使用します。

#[derive(Serialize, Deserialize, Debug)]
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() 呼び出しに連結します。次の表では、カウント操作をカスタマイズするために設定できるオプションについて説明しています。

オプション
説明

collation()

The collation to use for the operation.
Type: Collation

hint()

The index to use for the operation.
Type: Hint

comment()

The comment to attach to the operation.
Type: Bson

limit()

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

max_time()

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

read_concern()

The read concern to use for the operation.
Type: ReadConcern

skip()

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

selection_criteria()

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() メソッドの動作を変更できます。次の表では、推定カウント操作 をカスタマイズするために設定できるオプションについて説明します。

オプション
説明

max_time()

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

comment()

The comment to attach to the operation.
Type: Bson

read_concern()

The read concern to use for the operation.
Type: ReadConcern

selection_criteria()

The read preference and tags to use for the operation.
Type: SelectionCriteria

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

戻る

返すフィールドを指定する

項目一覧