@@ -15,4 +15,95 @@ Choose a Connection Target
15
15
:local:
16
16
:backlinks: none
17
17
: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>`__ .
0 commit comments