Docs Menu

Docs HomeDevelop ApplicationsMongoDB DriversRust Driver

Quick Reference

On this page, you can see examples that use the Rust driver to perform several common MongoDB tasks. Each row of the following table describes the task, shows the driver syntax to execute the task, and includes links to the related reference and API documentation.

The Rust driver offers an asynchronous runtime for executing async applications. Additionally, the driver supports a blocking synchronous runtime. For each MongoDB task listed in the following table, you can see examples that use both the asynchronous and synchronous APIs.

Tip

To learn more about asynchronous and synchronous runtimes, see the Asynchronous and Synchronous APIs guide.

Command
Syntax
Find a Document

API Documentation

Async Runtime

let result = collection.find_one(
doc! { "title": "Peter Pan" },
None
).await?;

Sync Runtime

let result = collection.find_one(
doc! { "title": "Peter Pan" },
None
)?;

Async Runtime

let filter = doc! { "year": 1925 };
let mut cursor = collection.find(filter, None).await?;

Sync Runtime

let filter = doc! { "year": 1925 };
let mut cursor = collection.find(filter, None)?;

Async Runtime

let doc = doc! {
"title": "Mistress America", "type": "movie"
};
let result = collection.insert_one(doc, None).await?;

Sync Runtime

let doc = doc! {
"title": "Mistress America", "type": "movie"
};
let result = collection.insert_one(doc, None)?;

Async Runtime

let docs = vec![
doc! { "title": "Friends With Money", "runtime": 88 },
doc! { "title": "Please Give", "runtime": 90 },
doc! { "title": "You Hurt My Feelings", "runtime": 93 },
];
let result = collection.insert_many(docs, None).await?;

Sync Runtime

let docs = vec![
doc! { "title": "Friends With Money", "runtime": 88 },
doc! { "title": "Please Give", "runtime": 90 },
doc! { "title": "You Hurt My Feelings", "runtime": 93 },
];
let result = collection.insert_many(docs, None)?;

Async Runtime

let filter = doc! { "title": "Burn After Reading"};
let update =
doc! {
"$set": doc!{ "num_mflix_comments": 1 }
};
let result = collection.update_one(
filter, update, None
).await?;

Sync Runtime

let filter = doc! { "title": "Burn After Reading"};
let update =
doc! {
"$set": doc!{ "num_mflix_comments": 1 }
};
let result = collection.update_one(
filter, update, None
)?;

Async Runtime

let filter = doc! { "rated": "PASSED"};
let update =
doc! {
"$set": doc!{ "rated": "Not Rated" }
};
let result = collection.update_many(
filter, update, None
).await?;

Sync Runtime

let filter = doc! { "rated": "PASSED"};
let update =
doc! {
"$set": doc!{ "rated": "Not Rated" }
};
let result = collection.update_many(
filter, update, None
)?;

Async Runtime

let filter = doc! { "title": "è Nous la Libertè" };
let replacement =
doc! {
"title": "À nous la liberté",
"type": "movie",
"directors": vec! [ "René Clair" ]
};
let result = collection.replace_one(
filter, replacement, None
).await?;

Sync Runtime

let filter = doc! { "title": "è Nous la Libertè" };
let replacement =
doc! {
"title": "À nous la liberté",
"type": "movie",
"directors": vec! [ "René Clair" ]
};
let result = collection.replace_one(
filter, replacement, None
)?;

Async Runtime

let filter = doc! { "title": "Search and Destroy" };
let result = collection.delete_one(filter, None).await?;

Sync Runtime

let filter = doc! { "title": "Search and Destroy" };
let result = collection.delete_one(filter, None)?;

Async Runtime

let filter = doc! {
"year": doc! { "$lt": 1920 }
};
let result = collection.delete_many(filter, None).await?;

Sync Runtime

let filter = doc! {
"year": doc! { "$lt": 1920 }
};
let result = collection.delete_many(filter, None)?;
Access Data from a Cursor Iteratively

Async Runtime

let mut cursor = collection.find(
doc! { "$and": vec!
[
doc! { "metacritic": doc! { "$gt": 90 } },
doc! { "directors": vec! [ "Martin Scorsese" ] }
] },
None
).await?;
while let Some(result) = cursor.try_next().await? {
println!("{}", result);
};

Sync Runtime

let cursor = collection.find(
doc! { "$and": vec!
[
doc! { "metacritic": doc! { "$gt": 90 } },
doc! { "directors": vec! [ "Martin Scorsese" ] }
] },
None
)?;
for result in cursor {
println!("{}", result?);
}
Access Data from a Cursor as an Array

Async Runtime

let cursor = collection.find(
doc! { "title": "Secrets & Lies" }, None
).await?;
let results: Vec<Document> = cursor.try_collect().await?;

Sync Runtime

let cursor = collection.find(
doc! { "title": "Secrets & Lies" }, None
)?;
let results: Vec<Result<Document>> = cursor.collect();

Async Runtime

let filter = doc! {
"languages": vec! [ "Mandarin" ]
};
let result = collection.count_documents(filter, None).await?;

Sync Runtime

let filter = doc! {
"languages": vec! [ "Mandarin" ]
};
let result = collection.count_documents(filter, None)?;
List Distinct Values of a Field

Async Runtime

let field_name = "title";
let filter = doc! {
"directors": vec! [ "Sean Baker" ]
};
let results = collection.distinct(
field_name, filter, None
).await?;

Sync Runtime

let field_name = "title";
let filter = doc! {
"directors": vec! [ "Sean Baker" ]
};
let results = collection.distinct(
field_name, filter, None
)?;
Limit the Number of Documents Retrieved

Async Runtime

let opts: FindOptions = FindOptions::builder()
.limit(5)
.build();
let filter = doc! { "awards.wins": 25};
let mut cursor = collection.find(filter, opts).await?;

Sync Runtime

let opts: FindOptions = FindOptions::builder()
.limit(5)
.build();
let filter = doc! { "awards.wins": 25};
let mut cursor = collection.find(filter, opts)?;
Skip Retrieved Documents

Async Runtime

let opts: FindOptions = FindOptions::builder()
.skip(1)
.build();
let filter = doc! { "runtime": 100 };
let mut cursor = collection.find(filter, opts).await?;

Sync Runtime

let opts: FindOptions = FindOptions::builder()
.skip(1)
.build();
let filter = doc! { "runtime": 100 };
let mut cursor = collection.find(filter, opts)?;
Sort the Documents When Retrieving Them

Async Runtime

let opts: FindOptions = FindOptions::builder()
.sort(doc! { "imdb.rating": 1 })
.build();
let filter = doc! {
"directors": vec! [ "Nicole Holofcener" ]
};
let mut cursor = collection.find(filter, opts).await?;

Sync Runtime

let opts: FindOptions = FindOptions::builder()
.sort(doc! { "imdb.rating": 1 })
.build();
let filter = doc! {
"directors": vec! [ "Nicole Holofcener" ]
};
let mut cursor = collection.find(filter, opts)?;
Project Document Fields When Retrieving Them

Async Runtime

let opts: FindOptions = FindOptions::builder()
.projection(doc! { "title": 1, "metacritic": 1, "_id": 0 })
.build();
let filter = doc! { "year": 2015 };
let mut cursor = collection.find(filter, opts).await?;

Sync Runtime

let opts: FindOptions = FindOptions::builder()
.projection(doc! { "title": 1, "metacritic": 1, "_id": 0 })
.build();
let filter = doc! { "year": 2015 };
let mut cursor = collection.find(filter, opts)?;
Create an Index

Async Runtime

let index: IndexModel = IndexModel::builder()
.keys(doc! { "title": 1 })
.build();
let result = collection.create_index(index, None).await?;

Sync Runtime

let index: IndexModel = IndexModel::builder()
.keys(doc! { "title": 1 })
.build();
let result = collection.create_index(index, None)?;
← Next Steps