How to Connect to MongoDB Database Clusters

MongoDB is a source-available cross-platform document-oriented database program for high-volume storage. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas.

You can connect to DigitalOcean Managed Databases using command line tools and other third-party clients. This guide explains where to find your MongoDB database’s connection details and how to use them to configure tools and clients.

View MongoDB Cluster Connection Details

You use your database’s connection details to configure tools, applications, and resources that connect to the database. To view your database’s connection details, click the name of the cluster on the Databases page to go to its Overview page.

Databases Overview screen showing connection string

You can view customized connection details based on how you want to connect to the database:

  • Public network and Private network (VPC) options generate connection details based on if you want to connect via the cluster’s public hostname or the cluster’s private hostname. Only other resources in the same VPC network as the cluster can access it using its private hostname.

  • The Database field updates the connection details based on which database you want to connect to.

  • The User field updates the connection details with the user credentials that you would like to connect with.

You can also choose to view the connection details in three different formats:

  • Connection parameters: Database information meant for application configuration, such as Studio 3T.

  • Connection string: A condensed string that you can pass to a client, such as MongoDB Compass.

  • Flags: A complete mongo command that supplies the connection variables as individual flags.

We recommend the flags format because the readability can help if you want to customize the way you connect.

By default, the control panel doesn’t reveal the cluster’s password for security reasons. Click Copy to copy connection details with the password, or click show-password to reveal the password. Once you create a password for a user, you can only view it during the session you created it. We recommend storing your database passwords locally. You can reset a user password at anytime.

Note

Cluster hostnames do not resolve using standard dig requests to the hostname in the connection string. MongoDB clusters are hosted on multiple nodes and each has its own hostname. To retrieve the node hostnames of a cluster using dig, you must specify the srv record type in the request and prepend _mongodb._tcp. to the hostname in the connection string like this:

dig srv _mongodb._tcp.<cluster-hostname>

A properly formatted dig request looks like this:

dig srv _mongodb._tcp.db-mongodb-ams3-05678-466b0cd2.mongo.ondigitalocean.com

Connect to the Database

You can connect and manage the database using a command line tool or an application that has a graphic user interface (GUI). In this guide, we show you how to connect to the database using the command line tool, MongoDB 4.4 Community Edition, and the visual database management application, MongoDB Compass.

Any client you use to connect to your cluster must support MongoDB 4.2 or above in order to secure the connection using TLS encryption. Clients that do not support at least MongoDB 4.2 or above will not connect to the cluster.

To connect to MongoDB database clusters using MongoDB Community Edition, you need four things:

Your cluster’s connection details come with a preconfigured connection command that you can copy and paste into a terminal and use to connect to the cluster. The command contains your cluster’s network information and database user credentials.

To connect using this command, click the name of the cluster you want to connect to on the Databases page to go to its Overview page. In the CONNECTION DETAILS section, select Flags from the drop-down menu, then copy and paste the entire command from the control panel into your terminal. An example command looks like this:

mongo "mongodb+srv://doadmin:[email protected]ocean.com/admin?authSource=admin&replicaSet=db-mongodb-nyc3-67368" --tls --tlsCAFile PATH_TO_CA_CERTIFICATE

You need to manually edit the connection command to include the user password and the path to the cluster’s CA certificate that you downloaded to your local machine. We only display user passwords for a short time after user creation. You can reset a user’s password at any time.

A completed connection command looks like this:

mongo "mongodb+srv://doadmin:[email protected]cean.com/admin?authSource=admin&replicaSet=db-mongodb-ams3-05678" --tls --tlsCAFile /Users/example-user/certificates/ca-certificate.crt
Tip
To save time when formatting the connection command in the future, we recommend storing the cluster’s password and the certificate’s path as environment variables on your local machine and then substituting the password and path values for the variable names in the connection command.

When you connect successfully, your terminal changes to the MongoDB prompt, which displays the name of the database you’re connected to, like db-mongodb-nyc1-80946:>.

Once connected to the database, you can change databases or users, execute queries, and perform other database administration tasks.

To connect to MongoDB database clusters using MongoDB Compass, you need four things:

To connect to your MongoDB database, open MongoDB Compass, then click New Connection in the left navigation menu.

MongoDB Compass New Connection screen with connection string pasted into field

In the Paste your connection string field, paste the connection string provided by DigitalOcean into the field. You may need to manually edit the connection string to include the user password as we only display user passwords for a short time after user creation. You can reset a user’s password at any time.

Once you have pasted the connection string into the field, click Fill in connection fields individually, then click the More Options tab.

MongoDB Compass New Connection screen with connection string pasted into field

In the More Options tab, select Server Validation from the SSL drop-down menu. A Certificate Authority field appears. In the Certificate Authority field, click Select and then select the CA certificate you downloaded from the control panel.

Once you have selected the certificate, click CONNECT. MongoDB Compass connects to the database.

Once connected to the database, you can change databases or users, execute queries, and perform other database administration tasks.

Connect to Standby Nodes as Read-Only Nodes

MongoDB allows you to connect to the primary node and shift your connection’s read traffic to the secondary nodes. This can lighten the workload of your primary node.

To connect to your cluster’s standby nodes and use them as read-only nodes, pass the cluster’s connection string with the readPreference option set to secondary, like this:

mongo "mongodb+srv://doadmin:[email protected]cean.com/admin?authSource=admin&replicaSet=db-mongodb-fra1-46825&readPreference=secondary" --tls --tlsCAFile <path-to-CA-cert>

The client connects to the primary node, but directs read queries to the standby nodes.

Note
When directing your queries to the standby nodes, there is a small chance that the standby nodes may be out of sync with the primary node and the latest data may not be available on the standby nodes yet.

See MongoDB’s official documentation for more information about read preferences.