@@ -28,8 +28,6 @@ or set up a runnable project with examples from our Usage Guide.
28
28
29
29
- `Source Code <https://github.com/mongodb/mongo-rust-driver/>`__
30
30
31
-
32
-
33
31
Installation
34
32
------------
35
33
@@ -40,82 +38,86 @@ See `Installation <https://github.com/mongodb/mongo-rust-driver#Installation>`__
40
38
Connect to MongoDB Atlas
41
39
------------------------
42
40
43
- Select from the :guilabel:`Sync` or :guilabel:`Async` tabs below for
44
- corresponding connection code samples.
45
-
46
- .. include:: /includes/atlas-connect-blurb.rst
41
+ You can use the connection snippets in the following tabs to test your
42
+ connection to your MongoDB deployment on Atlas. Select from the
43
+ :guilabel:`Sync` or :guilabel:`Async` tabs below for corresponding
44
+ connection code samples.
47
45
48
46
.. tabs::
49
47
50
48
.. tab:: Async
51
49
:tabid: rust-async
52
50
53
- The default async runtime used by the driver is ``tokio``. To use a
54
- different runtime, see
55
- `Configuring the async runtime <https://github.com/mongodb/mongo-rust-driver#configuring-the-async-runtime>`__.
51
+ .. note::
52
+
53
+ The default async runtime used by the driver is ``tokio``. To use a
54
+ different runtime, see
55
+ `Configuring the async runtime <https://github.com/mongodb/mongo-rust-driver#configuring-the-async-runtime>`__.
56
56
57
- .. code-block:: rust
57
+ .. literalinclude:: /includes/connection-snippets/scram/rust-connection-async.rs
58
+ :language: rust
58
59
59
- use mongodb::{bson::doc, options::ClientOptions, Client};
60
+ .. tab:: Sync
61
+ :tabid: rust-sync
60
62
61
- #[tokio::main]
62
- async fn main() -> mongodb::error::Result<()> {
63
- // Parse your connection string into an options struct
64
- let mut client_options =
65
- ClientOptions::parse("mongodb+srv://<username>:<password>@<cluster-url>/test?w=majority")
66
- .await?;
63
+ .. important::
64
+
65
+ You must enable the sync API in your application to use the
66
+ synchronous framework. See
67
+ `Enabling the sync API <https://github.com/mongodb/mongo-rust-driver#enabling-the-sync-api>`__
68
+ for more details.
67
69
68
- // Manually set an option
69
- client_options.app_name = Some("Rust Demo".to_string());
70
+ .. literalinclude:: /includes/connection-snippets/scram/rust-connection-sync.rs
71
+ :language: rust
72
+
73
+ This connection snippet uses the {+stable-api+} feature, which you can
74
+ enable when using the Rust driver v2.0 and later to connect to MongoDB
75
+ Server v5.0 and later. When you use this feature, you can update your driver or server without
76
+ worrying about backward compatibility issues with any commands covered by the
77
+ {+stable-api+}.
70
78
71
- // Get a handle to the cluster
72
- let client = Client::with_options(client_options)?;
79
+ To learn more about the {+stable-api+} feature, see the :manual:`Server
80
+ manual </reference/stable-api/>`.
73
81
74
- // Ping the server to see if you can connect to the cluster
75
- client
76
- .database("admin")
77
- .run_command(doc! {"ping": 1}, None)
78
- .await?;
79
- println!("Connected successfully.");
82
+ .. include:: /includes/stable-api-notice.rst
80
83
81
- // List the names of the databases in that cluster
82
- for db_name in client.list_database_names(None, None).await? {
83
- println!("{}", db_name);
84
- }
85
- Ok(())
86
- }
84
+ .. _connect-atlas-no-stable-api-php-driver:
87
85
88
- .. tab:: Sync
89
- :tabid: rust-sync
86
+ Connect to MongoDB Atlas Without the Stable API
87
+ -----------------------------------------------
90
88
91
- Make sure you enabled the sync API. See
92
- `Enabling the sync API <https://github.com/mongodb/mongo-rust-driver#enabling-the-sync-api>`__
93
- for more details.
89
+ If you are using a version of MongoDB or the driver that doesn't support the
90
+ {+stable-api+}, you can use the connection snippets in the following
91
+ tabs to test your connection to your MongoDB deployment on Atlas. Select from the
92
+ :guilabel:`Sync` or :guilabel:`Async` tabs below for corresponding
93
+ connection code samples.
94
94
95
- .. code-block:: rust
95
+ .. tabs::
96
96
97
- use mongodb::{bson::doc, sync::Client};
97
+ .. tab:: Async
98
+ :tabid: rust-async-no-stableapi
98
99
99
- fn main() -> mongodb::error::Result<()> {
100
- // Get a handle to the cluster
101
- let client = Client::with_uri_str(
102
- "mongodb+srv://<username>:<password>@<cluster-url>/test?w=majority",
103
- )?;
100
+ .. note::
101
+
102
+ The default async runtime used by the driver is ``tokio``. To use a
103
+ different runtime, see
104
+ `Configuring the async runtime <https://github.com/mongodb/mongo-rust-driver#configuring-the-async-runtime>`__.
104
105
105
- // Ping the server to see if you can connect to the cluster
106
- client
107
- .database("admin")
108
- .run_command(doc! {"ping": 1}, None)?;
109
- println!("Connected successfully.");
106
+ .. literalinclude:: /includes/connection-snippets/scram/rust-connection-no-stableapi-async.rs
107
+ :language: rust
108
+
109
+ .. tab:: Sync
110
+ :tabid: rust-sync-no-stableapi
110
111
111
- // List the names of the databases in that cluster
112
- for db_name in client.list_database_names(None, None)? {
113
- println!("{}", db_name);
114
- }
115
- Ok(())
116
- }
112
+ .. important::
113
+
114
+ You must enable the sync API in your application to use the
115
+ synchronous framework. See
116
+ `Enabling the sync API <https://github.com/mongodb/mongo-rust-driver#enabling-the-sync-api>`__
117
+ for more details.
117
118
118
- .. include:: /includes/serverless-compatibility.rst
119
+ .. literalinclude:: /includes/connection-snippets/scram/rust-connection-no-stableapi-sync.rs
120
+ :language: rust
119
121
120
122
Connect to a MongoDB Server on Your Local Machine
121
123
-------------------------------------------------
0 commit comments