Skip to content

DOCSP-49972 Go Choose a Connection Target #509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions source/fundamentals/connections.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Connections
.. toctree::

Connection Guide </fundamentals/connections/connection-guide>
Choose a Connection Target </fundamentals/connections/connection-target>
Connection Options </fundamentals/connections/connection-options>
Network Compression </fundamentals/connections/network-compression>
Configure TLS </fundamentals/connections/tls>
Expand All @@ -28,6 +29,7 @@ Learn how to use the {+driver-short+} to configure your application's
connection to a MongoDB deployment in the following sections:

- :ref:`golang-connection-guide`
- :ref:`golang-connection-targets`
- :ref:`golang-connection-options`
- :ref:`golang-network-compression`
- :ref:`golang-tls`
Expand Down
107 changes: 107 additions & 0 deletions source/fundamentals/connections/connection-target.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
.. _golang-connection-targets:

==========================
Choose a Connection Target
==========================

.. facet::
:name: genre
:values: tutorial

.. meta::
:description: Learn how to select connection targets for the MongoDB Go Driver.
:keywords:

.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol

Overview
--------

In this guide, you can learn how to use a connection string and a
``MongoClient`` object to connect to different types of MongoDB deployments.

.. tip::

To learn more about how to retrieve or format your connection string, see
the :ref:`Connection URI <golang-connection-uri>` guide in
the Go Connection Guide.

Atlas
-----

To connect to a MongoDB deployment on Atlas, include the following elements in your
connection string:

- URL of your Atlas cluster
- MongoDB username
- MongoDB password

Then, pass your connection string to the ``MongoClient`` constructor.

When you connect to Atlas, we recommend using the {+stable-api+} client option to avoid
breaking changes when Atlas upgrades to a new version of {+mdb-server+}.
To learn more about the {+stable-api+} feature, see the :ref:`<stable-api>`
guide.

The following code shows how to use the {+driver-short+} to connect to an Atlas cluster. The
code also uses the ``server_api`` field to specify a {+stable-api+} version.

.. _go-connection-example-code:

.. literalinclude:: /includes/fundamentals/code-snippets/srv.go
:language: go

Local Deployments
-----------------

.. include:: /includes/localhost-connection.rst

Replica Set
-----------

A MongoDB replica set deployment is a group of connected instances that
store the same set of data. This configuration provides data
redundancy and high data availability.

To connect to a replica set deployment, specify the hostname and port numbers
of each instance, separated by commas, and the replica set name as the value
of the ``replicaSet`` parameter in the connection string. In the following
example, the hostnames are ``host1``, ``host2``, and ``host3``, and the
port numbers are all ``27017``. The replica set name is ``myRS``.

.. code-block:: none

mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myRS

When connecting to a replica set, the driver takes the following actions by default:

- Discovers all replica set members when given the address of any one member.
- Dispatches operations to the appropriate member, such as instructions
to write against the **primary**.

.. tip::

You can specify just one host to connect to a replica set.
However, to ensure connectivity when the specified host
is unavailable, you should provide the full list of hosts.

Check failure on line 90 in source/fundamentals/connections/connection-target.txt

View workflow job for this annotation

GitHub Actions / TDBX Vale rules

[vale] reported by reviewdog 🐶 [MongoDB.AvoidSubjunctive] Avoid the subjunctive 'should'. Raw Output: {"message": "[MongoDB.AvoidSubjunctive] Avoid the subjunctive 'should'.", "location": {"path": "source/fundamentals/connections/connection-target.txt", "range": {"start": {"line": 90, "column": 24}}}, "severity": "ERROR"}

Direct Connection
`````````````````

To force operations on the host designated in the connection URI,
specify the ``directConnection`` option. Direct connections exhibit the
following behavior:

- They don't support SRV strings.
- They fail on writes when the specified host is not the **primary**.
- They require you to specify a :manual:`secondary read preference
</core/read-preference/#mongodb-readmode-secondary>` when the
specified host isn't the **primary** node.

.. note:: Replica Set in Docker

.. sharedinclude:: dbx/docker-replica-set.rst
Loading