MongoDB Rust Driver¶
Introduction¶
This is the official MongoDB Rust Driver.
Installation¶
See Installation
Connect to MongoDB Atlas¶
Select from the Sync or Async tabs below for corresponding connection code samples.
To connect to a MongoDB Atlas cluster, use the Atlas connection string for your cluster:
The default async runtime used by the driver is tokio
. To use a
different runtime, see
Configuring the async runtime.
use mongodb::{bson::doc, options::ClientOptions, Client}; async fn main() -> mongodb::error::Result<()> { // Parse your connection string into an options struct let mut client_options = ClientOptions::parse("mongodb+srv://<username>:<password>@<cluster-url>/test?w=majority") .await?; // Manually set an option client_options.app_name = Some("Rust Demo".to_string()); // Get a handle to the cluster let client = Client::with_options(client_options)?; // Ping the server to see if you can connect to the cluster client .database("admin") .run_command(doc! {"ping": 1}, None) .await?; println!("Connected successfully."); // List the names of the databases in that cluster for db_name in client.list_database_names(None, None).await? { println!("{}", db_name); } Ok(()) }
Compatibility¶
MongoDB Compatibility¶
The Rust driver is currently missing some features such as explicit sessions which prevents you from creating transactions and ensuring causal consistency. We plan to make this driver consistent with our other drivers in the future.
Rust Driver | MongoDB 4.4 | MongoDB 4.2 | MongoDB 4.0 | MongoDB 3.6 |
---|---|---|---|---|
1.1 | ✓ (*) | ✓ | ✓ | ✓ |
1.0 | ✓ (*) | ✓ | ✓ | ✓ |
The Rust driver is not compatible with MongoDB server versions older than 3.6.
(*) Not all features in MongoDB 4.4 are available in this version of the driver including OCSP. These features will be included in future versions of the driver.
Language Compatibility¶
The MongoDB Rust driver requires Rust 1.43 or later.
For more information on how to read the compatibility tables, see our guide on MongoDB Compatibility Tables.
How to get help¶
- Ask questions on our MongoDB Community Forums.
- Visit our Support Channels.
- See our RUST JIRA project to raise issues or request features.