Skip to content

Commit e48b2bf

Browse files
committed
add page content and link to manual
1 parent f44f352 commit e48b2bf

File tree

1 file changed

+187
-1
lines changed

1 file changed

+187
-1
lines changed

source/connect/mongoclient.txt

Lines changed: 187 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,191 @@ Create a MongoClient
1010
:depth: 2
1111
:class: singlecol
1212

13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
.. meta::
18+
:keywords: connection string, URI, Atlas, code example
1319

14-
.. TODO
20+
Overview
21+
--------
22+
23+
In this guide, you can learn how to connect to a `MongoDB Atlas deployment
24+
<https://www.mongodb.com/docs/atlas>`__, a MongoDB instance, or a replica set
25+
using the {+driver-short+}.
26+
27+
To connect to a MongoDB deployment, you need the following two things:
28+
29+
- **Connection URI**, also known as a *connection string*, which tells the {+driver-short+}
30+
which MongoDB deployment to connect to.
31+
32+
- **MongoClient** object, which creates the connection to and performs operations
33+
on the MongoDB deployment.
34+
35+
You can use ``options.Client()`` to customize the way the {+driver-short+}
36+
behaves while connected to MongoDB.
37+
38+
Connection URI
39+
--------------
40+
41+
A standard connection string includes the following components:
42+
43+
.. list-table::
44+
:widths: 20 80
45+
:header-rows: 1
46+
47+
* - Component
48+
- Description
49+
50+
* - ``mongodb://``
51+
52+
- Required. A prefix that identifies this as a string in the
53+
standard connection format.
54+
55+
* - ``username:password``
56+
57+
- Optional. Authentication credentials. If you include these, the client
58+
authenticates the user against the database specified in ``authSource``.
59+
For more information about the ``authSource`` connection option,
60+
see :ref:`golang-connection-troubleshooting` in the Connection Troubleshooting guide.
61+
62+
* - ``host[:port]``
63+
64+
- Required. The host and optional port number where MongoDB is running. If you don't
65+
include the port number, the driver uses the default port, ``27017``.
66+
67+
* - ``/defaultauthdb``
68+
69+
- Optional. The authentication database to use if the
70+
connection string includes ``username:password@``
71+
authentication credentials but not the ``authSource`` option.
72+
When you call ``client.db()`` with no argument, this is the database that is used. If you don't include
73+
this component, the client authenticates the user against the ``admin`` database.
74+
75+
* - ``?<options>``
76+
77+
- Optional. A query string that specifies connection-specific
78+
options as ``<name>=<value>`` pairs. See
79+
:ref:`golang-connection-options` for a full description of
80+
these options.
81+
82+
For more information about creating a connection string, see :manual:`Connection
83+
Strings </reference/connection-string>` in the MongoDB Server documentation.
84+
85+
Atlas Connection Example
86+
------------------------
87+
88+
To connect to MongoDB, you must create a client. A client manages your
89+
connections and runs database commands.
90+
91+
.. tip:: Reuse Your Client
92+
93+
We recommend that you reuse your client across sessions and operations. You
94+
can use the same ``Client`` instance to perform multiple tasks, instead of
95+
creating a new one each time. The ``Client`` type is safe for concurrent use
96+
by multiple `goroutines <https://go.dev/tour/concurrency/1>`__. To learn more
97+
about how connection pools work in the driver, see the
98+
:ref:`golang-connection-pools` guide.
99+
100+
You can create a client that uses your connection string and other client
101+
options by passing a ``ClientOptions`` object to the ``Connect()`` method.
102+
103+
To specify your connection URI, pass it to the ``ApplyURI()`` method, which
104+
returns a new ``ClientOptions`` instance. To set any other options, call the
105+
relevant helper method from the options package.
106+
107+
To learn more about connection options, see the :ref:`Connection Options section
108+
<golang-connection-options>`. To learn more about creating a client, see the API
109+
documentation for `Client <{+api+}/mongo#Client>`__ and `Connect()
110+
<{+api+}/mongo#Connect>`__.
111+
112+
You can set the {+stable-api+} version as an option to avoid breaking changes
113+
when you upgrade to a new server version. To learn more about the {+stable-api+}
114+
feature, see the :ref:`{+stable-api+} page <golang-stable-api>`.
115+
116+
The following code shows how you can create a client that uses an Atlas
117+
connection string and the {+stable-api+} version, connect to MongoDB, and verify
118+
that the connection is successful:
119+
120+
.. _go-connection-example-code:
121+
122+
.. literalinclude:: /includes/fundamentals/code-snippets/srv.go
123+
:language: go
124+
:copyable:
125+
:dedent:
126+
127+
.. tip::
128+
129+
Follow the :ref:`Quick Start guide <golang-connect-to-your-cluster>`
130+
to learn how to retrieve your Atlas connection string.
131+
132+
.. note::
133+
134+
To learn about connecting to Atlas Serverless, see the
135+
:ref:`Serverless Instance Limitations page
136+
<atlas-serverless-drivers>` to identify the minimum driver version
137+
required.
138+
139+
Other Ways to Connect to MongoDB
140+
--------------------------------
141+
142+
If you are connecting to a single MongoDB server instance or replica set
143+
that is not hosted on Atlas, see the following sections to find out how to
144+
connect.
145+
146+
Connect to a MongoDB Server on Your Local Machine
147+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
148+
149+
.. include:: /includes/localhost-connection.rst
150+
151+
To test whether you can connect to your server, replace the connection
152+
string with your localhost connection string in the preceding code example.
153+
154+
Connect to a Replica Set
155+
~~~~~~~~~~~~~~~~~~~~~~~~
156+
157+
A MongoDB replica set deployment is a group of connected instances that
158+
store the same set of data. This configuration provides data
159+
redundancy and high data availability.
160+
161+
To connect to a replica set deployment, specify the hostname and port numbers
162+
of each instance, separated by commas, and the replica set name as the value
163+
of the ``replicaSet`` parameter in the connection string. In the following
164+
example, the hostnames are ``host1``, ``host2``, and ``host3``, and the
165+
port numbers are all ``27017``. The replica set name is ``myRS``.
166+
167+
.. code-block:: none
168+
169+
mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myRS
170+
171+
When connecting to a replica set, the driver takes the following actions by default:
172+
173+
- Discovers all replica set members when given the address of any one member.
174+
- Dispatches operations to the appropriate member, such as instructions
175+
to write against the **primary**.
176+
177+
.. tip::
178+
179+
You can specify just one host to connect to a replica set.
180+
However, to ensure connectivity when the specified host
181+
is unavailable, you should provide the full list of hosts.
182+
183+
To learn more about replication in MongoDB, see the :manual:`Replication </replication>` section of server manual.
184+
185+
Direct Connection
186+
`````````````````
187+
188+
To force operations on the host designated in the connection URI,
189+
specify the ``directConnection`` option. Direct connections exhibit the
190+
following behavior:
191+
192+
- They don't support SRV strings.
193+
- They fail on writes when the specified host is not the **primary**.
194+
- They require you to specify a :manual:`secondary read preference
195+
</core/read-preference/#mongodb-readmode-secondary>` when the
196+
specified host isn't the **primary** node.
197+
198+
.. note:: Replica Set in Docker
199+
200+
.. sharedinclude:: dbx/docker-replica-set.rst

0 commit comments

Comments
 (0)