Lesson 1: Using MongoDB Connection Strings / Learn

Press O for more options
5:09

Video Transcript (English)

Before we start working with our data, we need to connect to an Atlas cluster. To do this, we need our connection string!

The MongoDB connection string allows us to connect to our cluster and work with our data.

In this video, we’ll explain what a connection string is, its components, and where to find it in the Atlas UI.

Let’s get started.

MongoDB provides two formats for the connection string: a standard format and the SRV connection format.

By default, MongoDB Atlas deployments use the SRV connection format.

This indicates to the client, in our case, mongosh, that the hostname that follows corresponds to a DNS SRV record. mongosh will then query the DNS for running instances of MongoDB.

A connection string has this format. It consists of some mandatory fields and several optional fields enclosed in square brackets.

Understanding the structure of a connection string will help you reverse engineer it in case you don't have access to a source like Atlas in order to copy it. So let’s break it down into its individual parts.

MongoDB connection strings start with either mongodb:// or mongodb+srv://. The +srv prefix indicates the SRV connection string type, which MongoDB Atlas uses by default.

This format secures connections by automatically enabling TLS and simplifies the connection process by providing a list of hosts to connect to. With the SRV connection string type, the MongoDB drivers perform a DNS lookup to retrieve the SRV record using the domain name specified after the @ symbol. The SRV record lists the cluster nodes and their ports, while a related TXT record provides additional connection options for the client.

By contrast, when connecting to a server that is not configured to use the SRV connection string type, we use the standard connection string format. This is where we specify the hostnames and port number separated by a colon.

Next, there’s the username and password. These are optional but must be provided if access control is enabled. In Atlas, access control is enabled by default and can’t be disabled. We cover access control in more detail in our unit on security.

Next, we see the default authentication database. This is an optional parameter that lets us specify the authentication database to check the user credentials against.

Here, we’re telling MongoDB to use the my_ldapdb database for authentication. By default, MongoDB will check against the admin cluster if a database is not specified.

For more information about authentication, check out our unit on securing your MongoDB database.

Finally, the connection string includes an option to provide a query string that contains connection-specific options as name-value pairs.

The default connection options allow you to securely connect and interact with your cluster. But you can use the query string to fine-tune the connection behavior.

In this example, retryWrites is set to true, and the write concern option is set to majority.

Check out our documentation for a full list of options and explanations of what each option does.

With all that out of the way, let’s show you where to locate the connection string in your Atlas dashboard.

Make sure you’re logged in to Atlas, and then navigate to the projects dashboard, which shows all the clusters in our project.

Let’s look for the connection string to our Analytics database. Click the Connect button.

Here we see the various options for connecting to our Atlas cluster. The connection string may look slightly different depending on the option we choose.

Since we're connecting from the MongoDB Shell, we click the Shell option.

The Atlas UI makes using our connection string with mongosh convenient for us.

The full command, including the connection string, appears in this window. Since we already have the latest version of mongosh installed, we leave the top two settings at their default values, and we find the connection string.

The connection string in this command is similar to what we saw earlier, but let’s take a quick look at the differences.

The connection string is preceded by the mongosh command, which starts the MongoDB Shell.

Like before, “mongodb” identifies this as a MongoDB connection string. And “+srv” sets the TLS option to true and tells MongoDB to use the DNS seed list.

Then we have the host, or address of the Atlas cluster you’re connecting to.

The Atlas-provided command also includes two optional flags for mongosh. The apiVersion flag specifies the API version of the MongoDB Shell. This ensures the MongoDB Shell and the server use the same API version when connecting.

Finally, the username flag details the username to be used for authentication.

Great work! In this video, you learned the components of a connection string and where to locate it in Atlas.

Now we can get started installing and connecting the MongoDB Shell. See you in the next video!