Skip to content

Commit f70fa00

Browse files
authored
DOCSP-28561: update rust connection snippets with stable api (#861)
* DOCSP-28561: update rust connection snippets with stable api * MW PR fixes 1 * port changes to php
1 parent c263c8a commit f70fa00

File tree

7 files changed

+154
-63
lines changed

7 files changed

+154
-63
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use mongodb::{bson::doc, options::{ClientOptions, ServerApi, ServerApiVersion}, Client};
2+
3+
#[tokio::main]
4+
async fn main() -> mongodb::error::Result<()> {
5+
// Replace the placeholder with your Atlas connection string
6+
let uri = "<connection string>";
7+
let mut client_options =
8+
ClientOptions::parse(uri)
9+
.await?;
10+
11+
// Set the server_api field of the client_options object to Stable API version 1
12+
let server_api = ServerApi::builder().version(ServerApiVersion::V1).build();
13+
client_options.server_api = Some(server_api);
14+
15+
// Create a new client and connect to the server
16+
let client = Client::with_options(client_options)?;
17+
18+
// Send a ping to confirm a successful connection
19+
client
20+
.database("admin")
21+
.run_command(doc! {"ping": 1}, None)
22+
.await?;
23+
println!("Pinged your deployment. You successfully connected to MongoDB!");
24+
25+
Ok(())
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use mongodb::{bson::doc, options::ClientOptions, Client};
2+
3+
#[tokio::main]
4+
async fn main() -> mongodb::error::Result<()> {
5+
// Replace the placeholder with your Atlas connection string
6+
let uri = "<connection string>";
7+
let mut client_options =
8+
ClientOptions::parse(uri)
9+
.await?;
10+
11+
// Create a new client and connect to the server
12+
let client = Client::with_options(client_options)?;
13+
14+
// Send a ping to confirm a successful connection
15+
client
16+
.database("admin")
17+
.run_command(doc! {"ping": 1}, None)
18+
.await?;
19+
println!("Pinged your deployment. You successfully connected to MongoDB!");
20+
21+
Ok(())
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use mongodb::{bson::doc, sync::Client};
2+
3+
fn main() -> mongodb::error::Result<()> {
4+
// Replace the placeholder with your Atlas connection string
5+
let uri = "<connection string>";
6+
7+
// Create a new client and connect to the server
8+
let client = Client::with_uri_str(uri)?;
9+
10+
// Send a ping to confirm a successful connection
11+
client
12+
.database("admin")
13+
.run_command(doc! {"ping": 1}, None)?;
14+
println!("Pinged your deployment. You successfully connected to MongoDB!");
15+
16+
Ok(())
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use mongodb::{bson::doc, options::{ClientOptions, ServerApi, ServerApiVersion}, sync::Client};
2+
3+
fn main() -> mongodb::error::Result<()> {
4+
// Replace the placeholder with your Atlas connection string
5+
let uri = "<connection string>";
6+
let mut client_options =
7+
ClientOptions::parse(uri)?;
8+
9+
// Set the server_api field of the client_options object to Stable API version 1
10+
let server_api = ServerApi::builder().version(ServerApiVersion::V1).build();
11+
client_options.server_api = Some(server_api);
12+
13+
// Create a new client and connect to the server
14+
let client = Client::with_options(client_options)?;
15+
16+
// Send a ping to confirm a successful connection
17+
client
18+
.database("admin")
19+
.run_command(doc! {"ping": 1}, None)?;
20+
println!("Pinged your deployment. You successfully connected to MongoDB!");
21+
22+
Ok(())
23+
}

source/includes/stable-api-notice.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.. note::
22

3-
Starting from Feburary 2022, the **Versioned API** is known the **{+stable-api+}**.
3+
Starting from Feburary 2022, the **Versioned API** is known as the **{+stable-api+}**.
44
All concepts and features remain the same with this naming change.
55

source/php.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,15 @@ your MongoDB deployment on Atlas:
112112
.. literalinclude:: /includes/connection-snippets/scram/php-connection.php
113113
:language: php
114114

115-
This connection snippet uses the {+stable-api+} feature which you can use when
116-
connecting to MongoDB Server v5.0 and later and the PHP driver v1.9 and
117-
later.
118-
119-
When you use this feature, you can update your driver or server without
115+
This connection snippet uses the {+stable-api+} feature, which you can
116+
enable when using the PHP driver v1.9 and later to connect to MongoDB Server
117+
v5.0 and later. When you use this feature, you can update your driver or server without
120118
worrying about backward compatibility issues with any commands covered by the
121119
{+stable-api+}.
122120

121+
To learn more about the {+stable-api+} feature, see the :manual:`Server
122+
manual </reference/stable-api/>`.
123+
123124
.. include:: /includes/stable-api-notice.rst
124125

125126
.. _connect-atlas-no-stable-api-php-driver:

source/rust.txt

Lines changed: 59 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ or set up a runnable project with examples from our Usage Guide.
2828

2929
- `Source Code <https://github.com/mongodb/mongo-rust-driver/>`__
3030

31-
32-
3331
Installation
3432
------------
3533

@@ -40,82 +38,86 @@ See `Installation <https://github.com/mongodb/mongo-rust-driver#Installation>`__
4038
Connect to MongoDB Atlas
4139
------------------------
4240

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.
4745

4846
.. tabs::
4947

5048
.. tab:: Async
5149
:tabid: rust-async
5250

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>`__.
5656

57-
.. code-block:: rust
57+
.. literalinclude:: /includes/connection-snippets/scram/rust-connection-async.rs
58+
:language: rust
5859

59-
use mongodb::{bson::doc, options::ClientOptions, Client};
60+
.. tab:: Sync
61+
:tabid: rust-sync
6062

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.
6769

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+}.
7078

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/>`.
7381

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
8083

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:
8785

88-
.. tab:: Sync
89-
:tabid: rust-sync
86+
Connect to MongoDB Atlas Without the Stable API
87+
-----------------------------------------------
9088

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.
9494

95-
.. code-block:: rust
95+
.. tabs::
9696

97-
use mongodb::{bson::doc, sync::Client};
97+
.. tab:: Async
98+
:tabid: rust-async-no-stableapi
9899

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>`__.
104105

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
110111

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.
117118

118-
.. include:: /includes/serverless-compatibility.rst
119+
.. literalinclude:: /includes/connection-snippets/scram/rust-connection-no-stableapi-sync.rs
120+
:language: rust
119121

120122
Connect to a MongoDB Server on Your Local Machine
121123
-------------------------------------------------

0 commit comments

Comments
 (0)