44Connection Guide
55================
66
7+ .. contents:: On this page
8+ :local:
9+ :backlinks: none
10+ :depth: 2
11+ :class: singlecol
12+
713This guide shows you how to connect to a MongoDB instance or replica set
814using the Node.js driver.
915
@@ -18,19 +24,20 @@ example shows each part of the connection URI:
1824.. figure:: /includes/figures/connection-string-parts.png
1925 :alt: Each part of the connection string
2026
21- In this example, for the protocol, we use ``mongodb+srv`` which specifies the
22- :manual:`DNS Seedlist Connection Format
23- </reference/connection-string/#dns-seedlist-connection-format>`. This indicates
24- that the hostname following it corresponds to the DNS SRV record of your
25- MongoDB instance or deployment. If your instance or deployment does not have a
26- DNS SRV record, use ``mongodb`` to specify the :manual:`Standard Connection
27- Format </reference/connection-string/#standard-connection-string-format>`.
27+ In this example, we use ``mongodb`` for the protocol, which specifies
28+ the :manual:`Standard Connection String Format </reference/connection-string/#std-label-connections-standard-connection-string-format>`.
29+
30+ If your instance or deployment has a DNS SRV record, you can use the
31+ :manual:`DNS Seed List Connection Format
32+ </reference/connection-string/#dns-seed-list-connection-format>` for
33+ your connection string. This format offers more flexibility of
34+ deployment and the ability to change the servers in rotation without
35+ reconfiguring clients.
2836
2937.. note::
3038
31- If your deployment is on MongoDB Atlas, follow the
32- :atlas:`Atlas driver connection guide </driver-connection?tck=docs_driver_nodejs>`
33- to retrieve your connection string.
39+ To learn how to retrieve your connection string in Atlas, see the
40+ :atlas:`Atlas driver connection guide </driver-connection>`.
3441
3542The next part of the connection string contains your username and password
3643if you are using password-based authentication. Replace the value of ``user``
@@ -39,7 +46,7 @@ authentication mechanism that does not require a username and password, omit
3946this part of the connection URI.
4047
4148The next part of the connection string specifies the hostname or IP address and
42- port of your MongoDB instance. In the example above, we use ``sample-hostname ``
49+ port of your MongoDB instance. In the example above, we use ``sample.host ``
4350as the hostname and ``27017`` as the port. Replace these values to point to
4451your MongoDB instance.
4552
@@ -48,12 +55,12 @@ options as parameters. In the example above, we set two connection options:
4855``maxPoolSize=20`` and ``w=majority``. For more information on connection
4956options, skip to the :ref:`node-connection-options` section.
5057
51- .. _connect-atlas -node-driver:
58+ .. _connect-sample -node-driver:
5259
53- The code below shows how you can use the sample connection URI in a client to
60+ The code below shows how you can use a connection URI in a client to
5461connect to MongoDB.
5562
56- .. literalinclude:: /code-snippets/connection/srv .js
63+ .. literalinclude:: /code-snippets/connection/connect .js
5764 :language: javascript
5865
5966.. _node-other-ways-to-connect:
@@ -71,7 +78,7 @@ Connect to a MongoDB Server on Your Local Machine
7178.. include:: /includes/localhost-connection.rst
7279
7380To test whether you can connect to your server, replace the connection
74- string in the :ref:`Connect to MongoDB Atlas <connect-atlas -node-driver>` code
81+ string in the :ref:`Connect to MongoDB <connect-sample -node-driver>` code
7582example and run it.
7683
7784Connect to a Replica Set
@@ -94,10 +101,10 @@ When making a connection, the driver takes the following actions by default:
94101- Discovers all replica set members when given the address of any one member.
95102- Dispatches operations to the appropriate member, such as write against the **primary**.
96103
97- .. tip::
104+ .. tip:: Specify all hosts
98105
99- You only need to specify one host to connect to a replica set. However, to ensure connectivity if the specified host
100- is unavailable, provide the full list of hosts .
106+ To ensure connectivity if one host is unavailable, provide the full
107+ list of hosts when connecting to a replica set .
101108
102109Direct Connection
103110^^^^^^^^^^^^^^^^^
@@ -109,7 +116,7 @@ driver does not accept the DNS seedlist connection format (SRV) when you
109116specify this option.
110117
111118When you specify ``directConnection`` and connect to a secondary member of the
112- replica set, your write operations fail because it is not the primary member.
113- To perform read operations, you must enable secondary reads. See the
114- :manual:`read preference options </reference/connection-string/#read-preference-options>`
119+ replica set, your write operations fail because the client isn't
120+ connected to the primary member. To perform read operations, you must
121+ enable secondary reads. See the :manual:`read preference options </reference/connection-string/#read-preference-options>`
115122for more information.
0 commit comments