Skip to content

Commit e30ae08

Browse files
authored
DOCSP-48379 Connection Targets (#1043)
* DOCSP-48379 Connection Targets * edits * edits * last edits * one more * ignoring vale check * eol for file * NR review * vale check
1 parent db93fb6 commit e30ae08

File tree

2 files changed

+135
-1
lines changed

2 files changed

+135
-1
lines changed

source/connect/connection-targets.txt

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,95 @@ Choose a Connection Target
1515
:local:
1616
:backlinks: none
1717
:depth: 2
18-
:class: singlecol
18+
:class: singlecol
19+
20+
Overview
21+
--------
22+
23+
In this guide, you can learn how to use a connection string and a ``MongoClient``
24+
object to connect to different types of MongoDB deployments.
25+
26+
.. tip::
27+
28+
To learn more about how to retrieve your connection string, see
29+
the :atlas:`Connect via Drivers </driver-connection>` guide in
30+
the Atlas documentation.
31+
32+
Atlas
33+
-----
34+
35+
To connect to a MongoDB deployment on Atlas, include the following elements
36+
in your connection string:
37+
38+
- URL of your Atlas cluster
39+
- MongoDB username
40+
- MongoDB password
41+
42+
Then, pass your connection string to the ``MongoClient`` constructor.
43+
44+
When you connect to Atlas, we recommend using the {+stable-api+} client option to avoid
45+
breaking changes when Atlas upgrades to a new version of {+mdb-server+}.
46+
To learn more about the {+stable-api+} feature, see the :ref:`<nodejs-stable-api>`
47+
guide.
48+
49+
The following code shows how to use the {+driver-short+} to connect to an Atlas cluster. The
50+
code also uses the ``server_api`` field to specify a {+stable-api+} version.
51+
52+
.. literalinclude:: /includes/connect/connection-targets.js
53+
:language: javascript
54+
:start-after: start-connection-target-atlas
55+
:end-before: end-connection-target-atlas
56+
:dedent:
57+
58+
Local Deployments
59+
-----------------
60+
61+
To connect to a local standalone MongoDB deployment, specify the host of the
62+
server. Optionally, specify the port of the server. If no port is specified,
63+
the default port is ``27017``.
64+
65+
You can specify the host and port to connect to by using a
66+
connection string, as shown in the following code:
67+
68+
.. literalinclude:: /includes/connect/connection-targets.js
69+
:language: javascript
70+
:start-after: start-local-connection-uri
71+
:end-before: end-local-connection-uri
72+
:dedent:
73+
74+
You can also specify your host as ``localhost``. The following code example
75+
connects to ``localhost`` on the specified port:
76+
77+
.. literalinclude:: /includes/connect/connection-targets.js
78+
:language: javascript
79+
:start-after: start-localhost
80+
:end-before: end-localhost
81+
:dedent:
82+
83+
Replica Sets
84+
------------
85+
86+
To connect to a replica set, we recommend that you specify all nodes that are
87+
part of the replica set. If one or more nodes becomes unavailable,
88+
specifying all nodes allows the driver to still connect to the replica set
89+
if one node is available.
90+
91+
However, it is sufficient to pass the address of any one node in the replica set
92+
to the driver. The node does not need to be the primary, and it may be a hidden node.
93+
The driver will then automatically discover the remaining nodes.
94+
95+
The following example shows how to connect to the replica set by using a connection
96+
string and how to verify the replica set name on connection by using the
97+
``replicaSet`` connection string option:
98+
99+
.. literalinclude:: /includes/connect/connection-targets.js
100+
:language: javascript
101+
:start-after: start-replica-set-option
102+
:end-before: end-replica-set-option
103+
:dedent:
104+
105+
API Documentation
106+
-----------------
107+
108+
To learn more about creating a ``MongoClient`` object with the {+driver-short+},
109+
see the API documentation for `MongoClient <{+api+}/classes/MongoClient.html>`__ .
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// start-connection-target-atlas
2+
const { MongoClient, ServerApiVersion } = require("mongodb");
3+
4+
// Replace the placeholder with your Atlas connection string
5+
const uri = "<connection string>";
6+
7+
// Creates a MongoClient with a MongoClientOptions object to set the Stable API version
8+
const client = new MongoClient(uri, {
9+
serverApi: {
10+
version: ServerApiVersion.v1,
11+
strict: true,
12+
deprecationErrors: true,
13+
}
14+
}
15+
);
16+
17+
async function run() {
18+
try {
19+
// Connects the client to the server (optional starting in v4.7)
20+
await client.connect();
21+
22+
// Sends a ping to confirm a successful connection
23+
await client.db("admin").command({ ping: 1 });
24+
console.log("Pinged your deployment. You successfully connected to MongoDB!");
25+
} finally {
26+
// Ensures that the client will close when you finish/error
27+
await client.close();
28+
}
29+
}
30+
run().catch(console.dir);
31+
// end-connection-target-atlas
32+
33+
// start-local-connection-uri
34+
const client = new MongoClient("mongodb://host1:27017");
35+
// end-local-connection-uri
36+
37+
// start-localhost
38+
const client = new MongoClient("mongodb://localhost:27017");
39+
// end-localhost
40+
41+
// start-replica-set-option
42+
const client = new MongoClient("mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myRs");
43+
// end-replica-set-option

0 commit comments

Comments
 (0)