For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Connect to a Cluster via Client Libraries

The Connect dialog box for a cluster provides the details to connect to a cluster with an application using a MongoDB client library.


➤ Use the Select your language drop-down menu to set the language of the example on this page.


Your driver version must be compatible with your version of the MongoDB server. We recommend choosing the latest driver that is compatible with your MongoDB server version to use the latest database features and prepare for future version upgrades.

For a list of driver versions that contain the full set of functionality for your version of the MongoDB server, check the Client Library Compatibility Table for your driver.

Important

Officially Supported Drivers

Use officially supported MongoDB drivers at current versions for the best reliability and support experience. The Atlas SLA covers officially supported drivers only.

Atlas provides best-effort, diagnosis-only support for community drivers and end-of-life driver versions. Atlas does not test, patch, or certify community drivers. Support helps you determine whether connection issues originate from the platform or the driver, but does not debug driver code.

To learn more, see What is Atlas's support policy for community and unsupported drivers?.

You can connect to your sharded cluster using a driver and an optimized connection string.

Clients must support TLS to connect to an Atlas cluster.

Clients must support the SNI TLS extension to connect to an Atlas Free cluster (formerly known as M0) or Flex cluster.

To access a cluster, you must connect from an IP address on the Atlas project's IP access list. If you need to add an IP address to the IP access list, you can do so in the Connect dialog box. You can also add the IP address from the Network Access tab.

To access a cluster, you must have a database user with access to the desired databases on your Atlas cluster. Database users are separate from Atlas users. Database users have access to MongoDB databases, while Atlas users have access to the Atlas application itself.

You can create a database user to access your Atlas cluster in the Connect dialog box. You can also add the database user from the Cluster view. Atlas creates a database user when you create your first cluster.

1
  1. If it's not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.

  2. If it's not already displayed, select your desired project from the Projects menu in the navigation bar.

  3. In the sidebar, click Clusters under the Database heading.

The Clusters page displays.

2

Click Connect for the cluster to which you want to connect.

3

Choose Connection Type from the set of available buttons.

Note

Options Display if Feature Enabled

Atlas displays the connection type options after you enable Private IP for Peering, Private Endpoint, or both. If you haven't enabled either feature, no buttons display and Connection Type defaults to Standard.

Use this connection type for allowed public IP addresses.

4

Add a Connection IP Address

Important

Skip this step if Atlas indicates in the Setup connection security step that you have already configured an IP access list entry in your cluster. To manage the IP access list, see Add Entries to the Access List.

Atlas allows standard client connections to the cluster from entries in the project's IP access list. The project IP access list differs from the API access list, which restricts API access to specific IP or CIDR addresses.

If the IP access list is empty, Atlas prompts you to add an IP address to the project's IP access list. You can either:

  • Click Add Your Current IP Address to allow access from your current IP address.

  • Click Add an IP Address to add a single IP address or a CIDR-notated range of addresses.

Provide an optional description for the newly added IP address or CIDR range. Click Add IP Address to add the address to the IP access list.

5

Important

Skip this step if Atlas indicates in the Setup connection security step that you have at least one database user configured in your project. To manage existing database users, see Configure Database Users.

To access the cluster, you need a MongoDB user with access to the desired database or databases on the cluster in your project. If your project has no MongoDB users, Atlas prompts you to create a new user with the Atlas Admin role.

  1. Enter the new user's Username.

  2. Enter a Password for this new user or click Autogenerate Secure Password.

  3. Click Create Database User to save the user.

Use this user to connect to your cluster in the following step.

Once you have added an IP address to your IP access list and added a database user, click Choose Your Connection Method.

6

In the Choose a connection method step, select Drivers.

7

Select your driver and version from the dropdown menus. The code sample containing a connection string displays. Replace <password> with the password specified when you created your database user.

Under Use this connection string in your application, the connection string uses the SRV format (mongodb+srv://) by default. To view the standard (non-SRV) connection string (mongodb://), turn off the SRV Connection String toggle.

Note

If your passwords, database names, or connection strings contain reserved URI characters, you must escape the characters. For example, if your password is @bc123, you must escape the @ character when specifying the password in the connection string, such as %40bc123. To learn more, see Special Characters in Connection String Password.

To learn more, see Driver Compatibility.

In the following example, you authenticate and connect to an Atlas cluster by using a URI connection string. Replace the placeholders in the example with your credentials and deployment details.

Note

To connect to an Atlas M0 Free cluster or a Flex cluster, you must use a C driver version that supports MongoDB 7.0 and later. For complete documentation on compatibility between the C driver and MongoDB, see the MongoDB compatibility matrix. We recommend that you upgrade to the latest version of the driver.

client = mongoc_client_new ("mongodb+srv://<db_username>:<db_password>@<clusterName>.mongodb.net/?retryWrites=true&w=majority");
db = mongoc_client_get_database (client, "<databaseName>");
MongoDB Version
Recommended Driver Versions

All

See the MongoDB compatibility matrix for the latest recommended driver versions.

MongoDB 7.0 and later

Version 1.11 and later

Note

The following configuration options only apply if running the C driver in single-threaded mode.

MongoDB drivers automatically attempt server selection following a cluster election or failover event. By default, the C driver immediately raises an error if its first attempt to select a server fails. The following configuration settings may improve application connectivity to an Atlas cluster at the expense of spending more time in a server selection loop:

  • Set serverSelectionTryOnce to false to direct the C driver to perform server selection up to the time limit defined by serverSelectionTimeoutMS.

  • Lower the serverSelectionTimeoutMS to 15000 from the default of 30000. MongoDB elections typically take 10 seconds, but can be as fast as 5 seconds on Atlas. Setting this value to 15 seconds (15000 milliseconds) covers the upper bound of election plus additional time for latency.

If you are experiencing issues connecting to your cluster, see Troubleshoot Connection Issues.