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
/ /

Count Documents

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.

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:

#[derive(Serialize, Deserialize, Debug)]
struct Restaurant {
name: String,
cuisine: String,
}

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.

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

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

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

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

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

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

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

To learn more about any of the methods or types discussed in this guide, see the following API documentation:

Back

Specify Fields to Return

On this page