Docs Menu
Docs Home
/ / /
Rust Driver
/

Find Multiple Documents

On this page

  • Example
  • Output

You can query for multiple documents in a collection by calling the find() method on a Collection instance.

Pass a query filter to the find() method to return documents in the collection that match the filter. If you do not include a filter, MongoDB returns all the documents in the collection.

Tip

To learn more about retrieving documents, see the Retrieve Data guide, and to learn more about creating query filters, see the Specify a Query guide.

The find() method returns a Cursor type, which you can iterate through to retrieve individual documents. To learn more about using cursors, see the Access Data by Using a Cursor guide.

This example retrieves documents that match a query filter from the restaurants collection in the sample_restaurants database. The find() method returns all documents in which the value of the cuisine field is "French".

You can model each retrieved document as a 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>: Retrieves and prints collection documents as BSON documents

  • <Restaurant>: Retrieves and prints collection documents as instances of the Restaurant struct, defined at the top of the code

Select the Asynchronous or Synchronous tab to see the corresponding code for each runtime:

use mongodb::{
bson::doc,
Client,
Collection
};
use futures::TryStreamExt;
use serde::{ Deserialize, Serialize };
#[derive(Serialize, Deserialize, Debug)]
struct Restaurant {
name: String,
cuisine: String,
}
#[tokio::main]
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 mut cursor = my_coll.find(
doc! { "cuisine": "French" }
).await?;
while let Some(doc) = cursor.try_next().await? {
println!("{:#?}", doc);
}
Ok(())
}
use mongodb::{
bson::doc,
sync::{Client, Collection}
};
use serde::{ Deserialize, Serialize };
#[derive(Serialize, Deserialize, Debug)]
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 mut cursor = my_coll.find(
doc! { "cuisine": "French" }
).run()?;
for result in cursor {
println!("{:#?}", result?);
}
Ok(())
}

Select the BSON Document Results or Restaurant Struct Results tab to see the corresponding code output based on your collection's type parameter:

...
Some(
Document({
"_id": ObjectId(
"...",
),
...
"name": String(
"Cafe Un Deux Trois",
),
...
}),
),
Some(
Document({
"_id": ObjectId(
"...",
),
...
"name": String(
"Calliope",
),
...
}),
)
...
...
Restaurant {
name: "Cafe Un Deux Trois",
cuisine: "French",
}
Restaurant {
name: "Calliope",
cuisine: "French",
}
...

Back

Find One

On this page