==================
Connect to MongoDB
==================

Use the ``client.connect`` method to connect to a running MongoDB deployment.

Connect to a Single MongoDB Instance
------------------------------------

To connect to a single MongoDB instance, specify the URI of the MongoDB
instance to connect to.

In the following example, the
:manual:`URI connection string </reference/connection-string/>`
specifies connecting to a MongoDB instance that is running on
``localhost`` using port ``27017``. The ``myproject`` indicates the database
to use.

.. literalinclude:: /includes/basic-connection.js
   :language: js

For more information on the URI connection string, see
:manual:`URI connection string </reference/connection-string/>` .

Connect to a Replica Set
------------------------

To connect to a :manual:`replica set </core/replication-introduction/>` ,
include a seedlist of replica set members and the name of the replica set in the
:manual:`URI connection string </reference/connection-string/>` .

In the following example, the connection string specifies two of the replica set members running on ``localhost:27017`` and ``localhost:27018`` and the name of the replica set (\ ``foo``\ ).

.. literalinclude:: /includes/connect-to-replicaset.js
   :language: js

For more information on the URI connection string, see
:manual:`URI connection string </reference/connection-string/>` .

Connect to Sharded Cluster
--------------------------

To connect to a :manual:`sharded cluster </core/sharded-cluster-components/>` , specify the ``mongos`` instance or instances in the :manual:`URI connection string </reference/connection-string/>` .

In the following example, the connection string specifies the ``mongos`` instances running on ``localhost:50000`` and ``localhost:50001``.

.. literalinclude:: /includes/connect-to-sharded-cluster.js
   :language: js

For more information on the URI connection string, see
:manual:`URI connection string </reference/connection-string/>` .

Connection Options
------------------

You can specify various connection settings in the :manual:`URI connection string </reference/connection-string/>` .

For example, you can specify TLS/SSL and authentication setting.

.. code-block:: js

   const { MongoClient } = require('mongodb');
   const fs = require('fs');

   // Connection URL
   const url = 'mongodb://dave:password@localhost:27017?authMechanism=DEFAULT&authSource=db&tls=true';

   // Create a client, passing in additional options
   const client = new MongoClient(url,  {
     tlsCAFile: path.resolve(__dirname + '/certs/ca.pem'),
     tlsCertificateKeyFile: `${__dirname}/certs/client.pem`
   });

   // Function to connect to the server
   async function run() {
     try {
       // Connect the client to the server
       await client.connect();
       console.log('Connected successfully to server');
     } finally {
       // Ensures that the client will close when you finish/error
       await client.close();
     }
   }

   // Runs your code
   run();


For more information on connecting with authentication and TSL/SSL, see:


* :doc:`Authentication </tutorials/connect/authentication>` : detailed documentation of the various ways to specify authentication credentials
* :doc:`TLS/SSL </tutorials/connect/tls>` : Detailed documentation of the various ways to specify the properties of an TLS/SSL connection

For more information on the connection options:

* :manual:`URI connection string </reference/connection-string/>` : MongoDB connection string URI.
* :doc:`/connection-settings` : Reference on the driver-specific connection settings.
