|
| 1 | +.. _rust-language-center: |
| 2 | + |
| 3 | +.. include:: /includes/unicode-checkmark.rst |
| 4 | + |
| 5 | +=================== |
| 6 | +MongoDB Rust Driver |
| 7 | +=================== |
| 8 | + |
| 9 | +.. default-domain:: mongodb |
| 10 | + |
| 11 | +.. contents:: On this page |
| 12 | + :local: |
| 13 | + :backlinks: none |
| 14 | + :depth: 1 |
| 15 | + :class: twocols |
| 16 | + |
| 17 | +Introduction |
| 18 | +------------ |
| 19 | + |
| 20 | +This is the official MongoDB Rust Driver. |
| 21 | + |
| 22 | +.. admonition:: Alpha Release |
| 23 | + :class: important |
| 24 | + |
| 25 | + The MongoDB Rust Driver is currently in alpha. |
| 26 | + |
| 27 | +- `Usage Guide <https://github.com/mongodb/mongo-rust-driver#example-usage>`__ |
| 28 | + |
| 29 | +- `API Reference <https://docs.rs/mongodb/latest/mongodb/>`__ |
| 30 | + |
| 31 | +- `Changelog <https://github.com/mongodb/mongo-rust-driver/releases>`__ |
| 32 | + |
| 33 | +- `Source Code <https://github.com/mongodb/mongo-rust-driver/>`__ |
| 34 | + |
| 35 | + |
| 36 | +Installation |
| 37 | +------------ |
| 38 | + |
| 39 | +See `Installation <https://github.com/mongodb/mongo-rust-driver#Installation>`__ |
| 40 | + |
| 41 | + |
| 42 | +Connect to MongoDB Atlas |
| 43 | +------------------------ |
| 44 | + |
| 45 | +To connect to a :atlas:`MongoDB Atlas </>` cluster, use the :atlas:`Atlas connection string </driver-connection>` for your cluster: |
| 46 | + |
| 47 | +.. code-block:: rust |
| 48 | + |
| 49 | + use bson::doc; |
| 50 | + use mongodb::{options::ClientOptions, Client}; |
| 51 | + |
| 52 | + fn main() -> mongodb::error::Result<()> { |
| 53 | + // Parse a connection string into an options struct. |
| 54 | + let mut client_options = |
| 55 | + ClientOptions::parse("mongodb+srv://<username>:<password>@<cluster-url>/test?w=majority")?; |
| 56 | + |
| 57 | + // Manually set an option. |
| 58 | + client_options.app_name = Some("Rust Demo".to_string()); |
| 59 | + |
| 60 | + // Get a handle to the deployment. |
| 61 | + let client = Client::with_options(client_options)?; |
| 62 | + |
| 63 | + client |
| 64 | + .database("admin") |
| 65 | + .run_command(doc! {"ping": 1}, None)?; |
| 66 | + println!("Connected successfully."); |
| 67 | + |
| 68 | + // List the names of the databases in that deployment. |
| 69 | + for db_name in client.list_database_names(None)? { |
| 70 | + println!("{}", db_name); |
| 71 | + } |
| 72 | + Ok(()) |
| 73 | + } |
| 74 | + |
| 75 | +Compatibility |
| 76 | +------------- |
| 77 | + |
| 78 | +MongoDB Compatibility |
| 79 | +~~~~~~~~~~~~~~~~~~~~~ |
| 80 | + |
| 81 | +.. include:: /includes/mongodb-compatibility-table-rust.rst |
| 82 | + |
| 83 | +Language Compatibility |
| 84 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 85 | + |
| 86 | +.. include:: /includes/language-compatibility-table-rust.rst |
| 87 | + |
| 88 | +.. include:: /includes/help-links-rust.rst |
0 commit comments