Navigation

MongoDB Rust Driver

Introduction

This is the official MongoDB Rust Driver.

Alpha Release

The MongoDB Rust Driver is currently in alpha.

Installation

See Installation

Connect to MongoDB Atlas

To connect to a MongoDB Atlas cluster, use the Atlas connection string for your cluster:

use bson::doc;
use mongodb::{options::ClientOptions, Client};

fn main() -> mongodb::error::Result<()> {
    // Parse a connection string into an options struct.
    let mut client_options =
        ClientOptions::parse("mongodb+srv://<username>:<password>@<cluster-url>/test?w=majority")?;

    // Manually set an option.
    client_options.app_name = Some("Rust Demo".to_string());

    // Get a handle to the deployment.
    let client = Client::with_options(client_options)?;

    client
        .database("admin")
        .run_command(doc! {"ping": 1}, None)?;
    println!("Connected successfully.");

    // List the names of the databases in that deployment.
    for db_name in client.list_database_names(None)? {
        println!("{}", db_name);
    }
    Ok(())
}

Compatibility

MongoDB Compatibility

Rust DriverMongoDB 4.2MongoDB 4.0MongoDB 3.6MongoDB 3.4MongoDB 3.2MongoDB 3.0MongoDB 2.6
0.9.x

Language Compatibility

The MongoDB Rust driver requires Rust 1.39 or later.

How to get help