@@ -37,35 +37,80 @@ See `Installation <https://github.com/mongodb/mongo-rust-driver#Installation>`__
37
37
Connect to MongoDB Atlas
38
38
------------------------
39
39
40
+ Select from the :guilabel:`Sync` or :guilabel:`Async` tabs below for
41
+ corresponding connection code samples.
42
+
40
43
.. include:: /includes/atlas-connect-blurb.rst
41
44
42
- .. code-block:: rust
45
+ .. tabs::
46
+
47
+ .. tab:: Async
48
+ :tabid: rust-async
49
+
50
+ The default async runtime used by the driver is ``tokio``. To use a
51
+ different runtime, see
52
+ `Configuring the async runtime <https://github.com/mongodb/mongo-rust-driver#configuring-the-async-runtime>`__.
53
+
54
+ .. code-block:: rust
55
+
56
+ use mongodb::{bson::doc, options::ClientOptions, Client};
57
+
58
+ #[tokio::main]
59
+ async fn main() -> mongodb::error::Result<()> {
60
+ // Parse your connection string into an options struct
61
+ let mut client_options =
62
+ ClientOptions::parse("mongodb+srv://<username>:<password>@<cluster-url>/test?w=majority")
63
+ .await?;
64
+
65
+ // Manually set an option
66
+ client_options.app_name = Some("Rust Demo".to_string());
67
+
68
+ // Get a handle to the cluster
69
+ let client = Client::with_options(client_options)?;
70
+
71
+ // Ping the server to see if you can connect to the cluster
72
+ client
73
+ .database("admin")
74
+ .run_command(doc! {"ping": 1}, None)
75
+ .await?;
76
+ println!("Connected successfully.");
77
+
78
+ // List the names of the databases in that cluster
79
+ for db_name in client.list_database_names(None, None).await? {
80
+ println!("{}", db_name);
81
+ }
82
+ Ok(())
83
+ }
84
+
85
+ .. tab:: Sync
86
+ :tabid: rust-sync
43
87
44
- use bson::doc;
45
- use mongodb::{options::ClientOptions, Client};
88
+ Make sure you enabled the sync API. See
89
+ `Enabling the sync API <https://github.com/mongodb/mongo-rust-driver#enabling-the-sync-api>`__
90
+ for more details.
46
91
47
- fn main() -> mongodb::error::Result<()> {
48
- // Parse a connection string into an options struct.
49
- let mut client_options =
50
- ClientOptions::parse("mongodb+srv://<username>:<password>@<cluster-url>/test?w=majority")?;
92
+ .. code-block:: rust
51
93
52
- // Manually set an option.
53
- client_options.app_name = Some("Rust Demo".to_string());
94
+ use mongodb::{bson::doc, sync::Client};
54
95
55
- // Get a handle to the deployment.
56
- let client = Client::with_options(client_options)?;
96
+ fn main() -> mongodb::error::Result<()> {
97
+ // Get a handle to the cluster
98
+ let client = Client::with_uri_str(
99
+ "mongodb+srv://<username>:<password>@<cluster-url>/test?w=majority",
100
+ )?;
57
101
58
- client
59
- .database("admin")
60
- .run_command(doc! {"ping": 1}, None)?;
61
- println!("Connected successfully.");
102
+ // Ping the server to see if you can connect to the cluster
103
+ client
104
+ .database("admin")
105
+ .run_command(doc! {"ping": 1}, None)?;
106
+ println!("Connected successfully.");
62
107
63
- // List the names of the databases in that deployment.
64
- for db_name in client.list_database_names(None)? {
65
- println!("{}", db_name);
66
- }
67
- Ok(())
68
- }
108
+ // List the names of the databases in that cluster
109
+ for db_name in client.list_database_names(None, None)? {
110
+ println!("{}", db_name);
111
+ }
112
+ Ok(())
113
+ }
69
114
70
115
Compatibility
71
116
-------------
0 commit comments