Skip to content

Commit 058071d

Browse files
shuangelalindseymoore
authored andcommitted
DOCSP-48378 make mongoclient page (mongodb#1038)
* make mongoclient page * fix link: * fix link * add anchor to page to avoid broken link * nr feedback * nb feedback * monospace
1 parent 9b60866 commit 058071d

File tree

1 file changed

+60
-93
lines changed

1 file changed

+60
-93
lines changed

source/connect/mongoclient.txt

Lines changed: 60 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -18,54 +18,71 @@ Create a MongoClient
1818
:depth: 2
1919
:class: singlecol
2020

21-
This guide shows you how to connect to a
22-
`MongoDB Atlas deployment <https://www.mongodb.com/docs/atlas>`__,
23-
a MongoDB instance, or a replica set using the Node.js driver.
21+
Overview
22+
--------
23+
24+
To connect to a MongoDB deployment, you need two things:
25+
26+
- **Connection URI**, also known as a *connection string*, which tells the
27+
{+driver-short+} which MongoDB deployment to connect to.
28+
- **MongoClient** object, which creates the connection to and performs
29+
operations on the MongoDB deployment.
30+
31+
You can also use ``MongoClientOptions`` to customize the way the {+driver-short+} behaves
32+
while connected to MongoDB.
33+
34+
This guide shows you how to create a connection string and use a ``MongoClient`` object
35+
to connect to MongoDB.
2436

2537
.. _node-connection-uri:
2638

2739
Connection URI
2840
--------------
2941

30-
The **connection URI** is the set of instructions that the driver uses to connect to a
31-
MongoDB deployment. It tells the driver how to connect to MongoDB and how to behave
32-
while connected. The following example shows each part of the connection URI:
42+
A standard connection string includes the following components:
3343

34-
.. figure:: /includes/figures/dns_seedlist_connection_string_parts.png
35-
:alt: Each part of the connection string
44+
.. list-table::
45+
:widths: 20 80
46+
:header-rows: 1
3647

37-
In this example, we connect to an Atlas MongoDB deployment that has a
38-
DNS SRV record. For more details, see the
39-
:manual:`DNS Seed List Connection Format
40-
</reference/connection-string/#dns-seed-list-connection-format>`
41-
documentation. This format offers flexibility in deployment and the
42-
ability to change the servers in rotation without reconfiguring clients.
48+
* - Component
49+
- Description
4350

44-
.. note::
51+
* - ``mongodb://``
52+
53+
- Required. A prefix that identifies this as a string in the
54+
standard connection format.
4555

46-
To learn how to retrieve your connection string in Atlas, see the
47-
:atlas:`Atlas driver connection guide </driver-connection>`.
56+
* - ``username:password``
4857

49-
If you are connecting to an instance or replica set that does not have a
50-
DNS SRV address, you must use ``mongodb`` for the protocol, which specifies
51-
the :manual:`Standard Connection String Format
52-
</reference/connection-string/#std-label-connections-standard-connection-string-format>`.
58+
- Optional. Authentication credentials. If you include these, the client
59+
authenticates the user against the database specified in ``authSource``.
60+
For more information about the ``authSource`` connection option,
61+
see :ref:`node-troubleshooting-connection-admin` in the Connection Troubleshooting guide.
5362

54-
After the protocol, the next part of the connection string contains credentials
55-
if you are using password-based authentication. Replace the value of ``user``
56-
with your username and ``pass`` with your password. If you are using an
57-
authentication mechanism that does not require a username and password, omit
58-
this part of the connection URI.
63+
* - ``host[:port]``
5964

60-
The next part of the connection string specifies the hostname or IP address of
61-
your MongoDB instance, followed by the port number. In the example above, we use
62-
``sample.host`` as the hostname and ``27017`` as the port. Replace these values
63-
to point to your MongoDB instance.
65+
- Required. The host and optional port number where MongoDB is running. If you don't
66+
include the port number, the driver uses the default port, ``27017``.
6467

65-
The last part of the connection string contains connection and authentication
66-
options as parameters. In the example above, we set two connection options:
67-
``maxPoolSize=20`` and ``w=majority``. For more information on connection
68-
options, skip to the :ref:`node-connection-options` section.
68+
* - ``/defaultauthdb``
69+
70+
- Optional. The authentication database to use if the
71+
connection string includes ``username:password@``
72+
authentication credentials but not the ``authSource`` option.
73+
When you call ``client.db()`` with no argument, this is the database that is used. If you don't include
74+
this component, the client authenticates the user against the ``admin`` database.
75+
76+
* - ``?<options>``
77+
78+
- Optional. A query string that specifies connection-specific
79+
options as ``<name>=<value>`` pairs. See
80+
:ref:`node-connection-options` for a full description of
81+
these options.
82+
83+
For more information about creating a connection string, see
84+
:manual:`Connection Strings </reference/connection-string>` in the
85+
MongoDB Server documentation.
6986

7087
.. _connect-sample-node-driver:
7188

@@ -81,8 +98,8 @@ URI and a ``MongoClientOptions`` object.
8198
As each ``MongoClient`` represents a pool of connections to the
8299
database, most applications only require a single instance of a
83100
``MongoClient``, even across multiple requests. To learn more about
84-
how connection pools work in the driver, see the :ref:`FAQ page
85-
<node-faq-connection-pool>`.
101+
how connection pools work in the driver, see the :ref:`Connection Pools page
102+
<node-connection-pools>`.
86103

87104
Use the ``serverApi`` option in your ``MongoClientOptions`` object to
88105
enable the {+stable-api+} feature, which forces the server to run operations
@@ -102,63 +119,13 @@ verify that the connection is successful:
102119
Call the ``MongoClient.connect()`` method explicitly if you want to verify that the
103120
connection is successful.
104121

105-
To learn more about the Stable
106-
API feature, see the :ref:`{+stable-api+} page <nodejs-stable-api>`.
107-
108-
.. _node-other-ways-to-connect:
109-
110-
Other Ways to Connect to MongoDB
111-
--------------------------------
112-
113-
If you are connecting to a single {+mdb-server+} instance or replica set
114-
that is not hosted on Atlas, see the following sections to find out how to
115-
connect.
116-
117-
Connect to a {+mdb-server+} on Your Local Machine
118-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
119-
120-
.. include:: /includes/localhost-connection.rst
121-
122-
To test whether you can connect to your server, replace the connection
123-
string in the :ref:`Connect to MongoDB <connect-sample-node-driver>` code
124-
example and run it.
125-
126-
Connect to a Replica Set
127-
^^^^^^^^^^^^^^^^^^^^^^^^
128-
129-
A MongoDB replica set deployment is a group of connected instances that
130-
store the same set of data. This configuration of instances provides data
131-
redundancy and high data availability.
132-
133-
To connect to a replica set deployment, specify the hostname and port numbers
134-
of each instance, separated by a comma, and the replica set name as the value
135-
of the ``replicaSet`` parameter in the connection string.
136-
137-
.. code-block:: none
138-
139-
mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myRs
140-
141-
When making a connection, the driver takes the following actions by default:
142-
143-
- Discovers all replica set members when given the address of any one member.
144-
- Dispatches operations to the appropriate member, such as write against the **primary**.
145-
146-
.. tip:: Specify all hosts
147-
148-
To ensure connectivity if one host is unavailable, provide the full
149-
list of hosts when connecting to a replica set.
122+
To learn more about the {+stable-api+} feature, see the :ref:`{+stable-api+} page <nodejs-stable-api>`.
150123

151-
Direct Connection
152-
^^^^^^^^^^^^^^^^^
124+
API Documentation
125+
-----------------
153126

154-
To force your operations to run on the host specified in your connection
155-
URI, you can specify the ``directConnection`` connection option. If you
156-
specify this option, you must use the standard connection URI format. The
157-
driver does not accept the DNS seedlist connection format (SRV) when you
158-
specify this option.
127+
For more information about creating a ``MongoClient`` object with the
128+
{+driver-short+}, see the following API documentation:
159129

160-
When you specify ``directConnection`` and connect to a secondary member of the
161-
replica set, your write operations fail because the client isn't
162-
connected to the primary member. To perform read operations, you must
163-
enable secondary reads. See the :manual:`read preference options </reference/connection-string/#read-preference-options>`
164-
for more information.
130+
- `MongoClient <{+api+}/classes/MongoClient.html>`__
131+
- `MongoClientOptions <{+api+}/interfaces/MongoClientOptions.html>`__

0 commit comments

Comments
 (0)