Skip to content

Commit d179217

Browse files
add stableapi connection example (#590)
1 parent d2d4990 commit d179217

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const { MongoClient, ServerApiVersion } = require("mongodb");
2+
3+
// Replace the placeholders with your credentials and hostname
4+
const uri = "mongodb+srv://<username>:<password>@<hostname>/?retryWrites=true&w=majority";
5+
6+
// Create a MongoClient with a MongoClientOptions object to set the Stable API version
7+
const client = new MongoClient(uri, {
8+
serverApi: {
9+
version: ServerApiVersion.v1,
10+
strict: true,
11+
deprecationErrors: true,
12+
}
13+
}
14+
);
15+
16+
async function run() {
17+
try {
18+
// Connect the client to the server (optional starting in v4.7)
19+
await client.connect();
20+
21+
// Send a ping to confirm a successful connection
22+
await client.db("admin").command({ ping: 1 });
23+
console.log("Pinged your deployment. You successfully connected to MongoDB!");
24+
} finally {
25+
// Ensures that the client will close when you finish/error
26+
await client.close();
27+
}
28+
}
29+
run().catch(console.dir);

source/fundamentals/connection/connect.txt

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ reconfiguring clients.
3939
To learn how to retrieve your connection string in Atlas, see the
4040
:atlas:`Atlas driver connection guide </driver-connection>`.
4141

42-
The next part of the connection string contains your username and password
42+
The next part of the connection string contains your credentials
4343
if you are using password-based authentication. Replace the value of ``user``
4444
with your username and ``pass`` with your password. If you are using an
4545
authentication mechanism that does not require a username and password, omit
4646
this part of the connection URI.
4747

48-
The next part of the connection string specifies the hostname or IP address and
49-
port of your MongoDB instance. In the example above, we use ``sample.host``
50-
as the hostname and ``27017`` as the port. Replace these values to point to
51-
your MongoDB instance.
48+
The next part of the connection string specifies the hostname or IP address of
49+
your MongoDB instance, followed by the port number. In the example above, we use
50+
``sample.host`` as the hostname and ``27017`` as the port. Replace these values
51+
to point to your MongoDB instance.
5252

5353
The last part of the connection string contains connection and authentication
5454
options as parameters. In the example above, we set two connection options:
@@ -57,12 +57,27 @@ options, skip to the :ref:`node-connection-options` section.
5757

5858
.. _connect-sample-node-driver:
5959

60-
The code below shows how you can use a connection URI in a client to
61-
connect to MongoDB.
60+
Atlas Connection Example
61+
------------------------
6262

63-
.. literalinclude:: /code-snippets/connection/connect.js
63+
You must create a client to connect to a MongoDB deployment on Atlas. To create
64+
a client, construct an instance of ``MongoClient``, passing in your
65+
URI and a ``MongoClientOptions`` object.
66+
67+
Use the ``serverApi`` option in your ``MongoClientOptions`` object to
68+
enable the {+stable-api+} feature, which forces the server to run operations
69+
with behavior compatible with the specified API version.
70+
71+
The following code shows how you can specify the connection string and the
72+
{+stable-api+} client option when connecting to a MongoDB deployment on Atlas and
73+
verify that the connection is successful:
74+
75+
.. literalinclude:: /code-snippets/connection/stable-api-connect.js
6476
:language: javascript
6577

78+
To learn more about the Stable
79+
API feature, see the :ref:`{+stable-api+} page <nodejs-stable-api>`.
80+
6681
.. _node-other-ways-to-connect:
6782

6883
Other Ways to Connect to MongoDB

0 commit comments

Comments
 (0)