Overview
In this guide, you can learn how to use the Rust driver to retrieve an accurate
and estimated count of the number of documents in a collection. The count_documents()
method returns the exact number of documents that match a query filter or that exist in
a collection, and the estimated_document_count() method returns the estimated number
of documents in a collection.
Sample Data
The examples in this guide use the restaurants collection in the sample_restaurants
database from the Atlas sample datasets. To access this collection
from your Rust application, create a Client that connects to an Atlas
cluster and assign the following values to your my_coll variable.
Select the Asynchronous or Synchronous tab to see the corresponding code for each runtime:
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");
To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the Get Started with the Rust Driver guide.
The examples in this guide use the following Restaurant struct as
a model for documents in the restaurants collection:
struct Restaurant { name: String, cuisine: String, }
Retrieve an Accurate Count
Use the count_documents() method to count the number of documents that are in a
collection. To count the number of documents that match a specific search
criteria, pass a query filter document to the count_documents() method.
To learn more about specifying a query, see the Specify a Query guide.
Count All Documents
To return a count of all documents in the collection, pass an empty filter document to
the count_documents() method as shown in the following example. Select the
Asynchronous or Synchronous tab to see the corresponding code
for each runtime:
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 Specific Documents
To return a count of documents that match specific search criteria, pass your query
filter document to the count_documents() method.
The following example counts the number of documents in which the value of the name
field includes the string "Sunset". Select the Asynchronous or
Synchronous tab to see the corresponding code for each runtime:
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
Customize Count Behavior
You can modify the behavior of the count_documents() method by chaining
option methods to your count_documents() call before the await or
run() method call. The following table describes options you can set to
customize the count operation:
Option | Description |
|---|---|
| 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 |
Retrieve an Estimated Count
You can retrieve an estimate of the number of documents in a collection by calling
the estimated_document_count() method before the await or run() method
call. The method estimates the amount of
documents based on collection metadata, which can be faster than performing an
accurate count.
The following example estimates the number of documents in a collection. Select the Asynchronous or Synchronous tab to see the corresponding code for each runtime:
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
Customize Estimated Count Behavior
You can modify the behavior of the estimated_document_count() method by chaining
option methods to your estimated_document_count() call. The following table
describes options you can set to customize the estimated count operation:
Option | Description |
|---|---|
| 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 Documentation
To learn more about any of the methods or types discussed in this guide, see the following API documentation: