Skip to content

DOCSP-48379 Connection Targets #1043

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

Merged
merged 9 commits into from
Mar 26, 2025
Merged
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
93 changes: 92 additions & 1 deletion source/connect/connection-targets.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,95 @@
:local:
:backlinks: none
:depth: 2
:class: singlecol
: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 your connection string, see
the :atlas:`Connect via Drivers </driver-connection>` guide in

Check failure on line 29 in source/connect/connection-targets.txt

View workflow job for this annotation

GitHub Actions / TDBX Vale rules

[vale] reported by reviewdog 🐶 [MongoDB.AvoidObscure] Use 'through, by' instead of 'via'. Raw Output: {"message": "[MongoDB.AvoidObscure] Use 'through, by' instead of 'via'.", "location": {"path": "source/connect/connection-targets.txt", "range": {"start": {"line": 29, "column": 24}}}, "severity": "ERROR"}
the Atlas documentation.

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:`<nodejs-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.

.. literalinclude:: /includes/connect/connection-targets.js
:language: javascript
:start-after: start-connection-target-atlas
:end-before: end-connection-target-atlas
:dedent:

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

To connect to a local standalone MongoDB deployment, specify the host of the
server. Optionally, specify the port of the server. If no port is specified,
the default port is ``27017``.

You can specify the host and port to connect to by using a
connection string, as shown in the following code:

.. literalinclude:: /includes/connect/connection-targets.js
:language: javascript
:start-after: start-local-connection-uri
:end-before: end-local-connection-uri
:dedent:

You can also specify your host as ``localhost``. The following code example
connects to ``localhost`` on the specified port:

.. literalinclude:: /includes/connect/connection-targets.js
:language: javascript
:start-after: start-localhost
:end-before: end-localhost
:dedent:

Replica Sets
------------

To connect to a replica set, we recommend that you specify all nodes that are
part of the replica set. If one or more nodes becomes unavailable,
specifying all nodes allows the driver to still connect to the replica set
if one node is available.

However, it is sufficient to pass the address of any one node in the replica set
to the driver. The node does not need to be the primary, and it may be a hidden node.

Check failure on line 92 in source/connect/connection-targets.txt

View workflow job for this annotation

GitHub Actions / TDBX Vale rules

[vale] reported by reviewdog 🐶 [MongoDB.ConciseTerms] 'must' is preferred over 'need to'. Raw Output: {"message": "[MongoDB.ConciseTerms] 'must' is preferred over 'need to'.", "location": {"path": "source/connect/connection-targets.txt", "range": {"start": {"line": 92, "column": 34}}}, "severity": "ERROR"}
The driver will then automatically discover the remaining nodes.

The following example shows how to connect to the replica set by using a connection
string and how to verify the replica set name on connection by using the
``replicaSet`` connection string option:

.. literalinclude:: /includes/connect/connection-targets.js
:language: javascript
:start-after: start-replica-set-option
:end-before: end-replica-set-option
:dedent:

API Documentation
-----------------

To learn more about creating a ``MongoClient`` object with the {+driver-short+},
see the API documentation for `MongoClient <{+api+}/classes/MongoClient.html>`__ .
43 changes: 43 additions & 0 deletions source/includes/connect/connection-targets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// start-connection-target-atlas
const { MongoClient, ServerApiVersion } = require("mongodb");

// Replace the placeholder with your Atlas connection string
const uri = "<connection string>";

// Creates a MongoClient with a MongoClientOptions object to set the Stable API version
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
}
);

async function run() {
try {
// Connects the client to the server (optional starting in v4.7)
await client.connect();

// Sends a ping to confirm a successful connection
await client.db("admin").command({ ping: 1 });
console.log("Pinged your deployment. You successfully connected to MongoDB!");
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
}
run().catch(console.dir);
// end-connection-target-atlas

// start-local-connection-uri
const client = new MongoClient("mongodb://host1:27017");
// end-local-connection-uri

// start-localhost
const client = new MongoClient("mongodb://localhost:27017");
// end-localhost

// start-replica-set-option
const client = new MongoClient("mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myRs");
// end-replica-set-option
Loading