Skip to content

Commit beb8820

Browse files
authored
Merge Go Connection Target PR
DOCSP-49972 Choose a Connection Target
2 parents c7bb8c1 + 20a14f5 commit beb8820

File tree

1 file changed

+119
-1
lines changed

1 file changed

+119
-1
lines changed

source/connect/connection-targets.txt

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,122 @@ Choose a Connection Target
1313
:depth: 2
1414
:class: singlecol
1515

16-
.. TODO
16+
Overview
17+
--------
18+
19+
In this guide, you can learn how to use a connection string and a ``MongoClient``
20+
object to connect to different types of MongoDB deployments by using the Go driver.
21+
22+
.. tip::
23+
24+
To see how to create and configure your ``MongoClient`` object, see the :ref:`Create a Mongo Client <golang-mongoclient>` page.
25+
26+
.. _go-atlas-connection-target:
27+
28+
Connect to Atlas
29+
----------------
30+
31+
To connect to a MongoDB deployment on Atlas, include the following elements
32+
in your connection string:
33+
34+
- URL of your Atlas cluster
35+
- MongoDB username
36+
- MongoDB password
37+
38+
Then, pass your connection string to the ``MongoClient`` constructor.
39+
40+
When you connect to Atlas, we recommend using the {+stable-api+} client option to avoid
41+
breaking changes when Atlas upgrades to a new version of {+mdb-server+}.
42+
To learn more about the {+stable-api+} feature, see the :ref:`<golang-stable-api>`
43+
guide.
44+
45+
The following code shows how you can create a client that uses an Atlas
46+
connection string and the {+stable-api+} version, connect to MongoDB, and
47+
verify that the connection is successful:
48+
49+
.. _go-connection-example-code:
50+
51+
.. literalinclude:: /includes/fundamentals/code-snippets/srv.go
52+
:language: go
53+
54+
.. important::
55+
56+
New Serverless instances can no longer be created, and as of May 5 2025, all
57+
existing Serverless instances have been migrated. The `All Clusters
58+
<https://cloud.mongodb.com/v2#/clusters>`__ page in the Atlas UI shows which tiers
59+
your instances are migrated to based on usage. See the :ref:`Manage
60+
Serverless Instances <manage-serverless-instances>` page to learn more about
61+
how to manually handle existing Serverless instances.
62+
63+
.. _go-local-connection-target:
64+
65+
Connect to Local Deployments
66+
----------------------------
67+
68+
.. include:: /includes/localhost-connection.rst
69+
70+
To test whether you can connect to your server, replace the connection
71+
string with your localhost connection string in the preceding :ref:`code example
72+
<go-connection-example-code>`.
73+
74+
.. _go-replica-set-connection-target:
75+
76+
Connect to Replica Sets
77+
-----------------------
78+
79+
A MongoDB replica set deployment is a group of connected instances that
80+
store the same set of data. This configuration provides data
81+
redundancy and high data availability.
82+
83+
To connect to a replica set deployment, specify the hostname and port numbers
84+
of each instance, separated by commas, and the replica set name as the value
85+
of the ``replicaSet`` parameter in the connection string. In the following
86+
example connection string, the hostnames are ``host1``, ``host2``, and ``host3``, and the
87+
port numbers are all ``27017``. The replica set name is ``myRS``.
88+
89+
.. code-block:: none
90+
91+
mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myRS
92+
93+
When connecting to a replica set, the driver takes the following actions by default:
94+
95+
- Discovers all replica set members when given the address of any one member.
96+
- Dispatches operations to the appropriate member, such as instructions
97+
to write against the **primary**.
98+
99+
.. tip::
100+
101+
You can specify just one host to connect to a replica set. However, you must
102+
provide the full list of hosts to ensure connectivity when the specified host
103+
is unavailable.
104+
105+
.. _go-direct-connection-target:
106+
107+
Direct Connection
108+
~~~~~~~~~~~~~~~~~
109+
110+
To force operations on the host designated in the connection string,
111+
specify the ``directConnection`` option. Direct connections exhibit the
112+
following behavior:
113+
114+
- They don't support SRV strings.
115+
- They fail on writes when the specified host is not the **primary**.
116+
- They require you to specify a **secondary** node with :ref:`secondary read
117+
preference <read-preference-use-cases>` when the specified host isn't the
118+
**primary** node.
119+
120+
.. note:: Replica Set in Docker
121+
122+
.. sharedinclude:: dbx/docker-replica-set.rst
123+
124+
API Documentation
125+
-----------------
126+
127+
To learn more about connecting to different MongoDB instances with a
128+
``MongoClient``, see the following API Documentation:
129+
130+
- `MongoClient <https://mongodb.github.io/node-mongodb-native/6.17/classes/MongoClient.html>`__
131+
- `options <https://mongodb.github.io/node-mongodb-native/6.17/classes/MongoClient.html#options>`__
132+
- `connect <https://mongodb.github.io/node-mongodb-native/6.17/classes/MongoClient.html#connect>`__
133+
- `readPreference <https://mongodb.github.io/node-mongodb-native/6.17/classes/MongoClient.html#readPreference>`__
134+

0 commit comments

Comments
 (0)