Count Documents
You can count the number of documents in a collection by calling one of
the following methods on a Collection
instance:
count_documents(): counts the number of documents that match a query filter. To learn more about creating query filters, see the Specify a Query guide.
estimated_document_count(): estimates the total number of documents in a collection by using collection metadata.
Each method returns the count as a u64
instance.
Note
If you don't pass a filter to the count_documents()
method,
MongoDB counts the total number of documents in the collection.
Example
This example counts documents in the restaurants
collection of the
sample_restaurants
database. The example uses the estimated_document_count()
method
to count the total number of documents in the collection. Then, the
example uses the count_documents()
method to count the number of
documents in which the value of the name
field includes the string "Sunset"
.
You can access the documents in the restaurants
collection as instances
of the Document
type or a custom data type. To specify which data type represents
the collection's data, replace the <T>
type parameter on the highlighted
line with one of the following values:
<Document>
: Represents collection documents as BSON documents<Restaurant>
: Represents collection documents as instances of theRestaurant
struct, defined at the top of the code
Select the Asynchronous or Synchronous tab to see the corresponding code for each runtime:
use std::env; use mongodb::{ bson::{ doc, Document }, Client, Collection }; use serde::{ Deserialize, Serialize }; struct Restaurant { name: String, cuisine: String, } async fn main() -> mongodb::error::Result<()> { let uri = "<connection string>"; let client = Client::with_uri_str(uri).await?; // Replace <T> with the <Document> or <Restaurant> type parameter let my_coll: Collection<T> = client .database("sample_restaurants") .collection("restaurants"); let ct = my_coll.estimated_document_count().await?; println!("Number of documents: {}", ct); let ct = my_coll.count_documents(doc! { "name": doc! { "$regex": "Sunset" } }).await?; println!("Number of matching documents: {}", ct); Ok(()) }
// Your values might differ Number of documents: 25216 Number of matching documents: 10
use std::env; use mongodb::{ bson::{ Document, doc }, sync::{ Client, Collection } }; use serde::{ Deserialize, Serialize }; struct Restaurant { name: String, cuisine: String, } fn main() -> mongodb::error::Result<()> { let uri = "<connection string>"; let client = Client::with_uri_str(uri)?; // Replace <T> with the <Document> or <Restaurant> type parameter let my_coll: Collection<T> = client .database("sample_restaurants") .collection("restaurants"); let ct = my_coll.estimated_document_count().run()?; println!("Number of documents: {}", ct); let ct = my_coll .count_documents(doc! { "name": doc! { "$regex": "Sunset" } }) .run()?; println!("Number of matching documents: {}", ct); Ok(()) }
// Your values might differ Number of documents: 25216 Number of matching documents: 10