Lesson 1: Using MongoDB Connection Strings / Learn

Video Transcript (English)

Welcome back, team! In this video, you’ll learn how to use the MongoDB connection string, where to locate the connection string for an Atlas cluster, and what the string consists of.

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

It describes the hosts that will be used, and the options for connecting to a MongoDB database.

For example, the connection string can be used to connect from mongosh, MongoDB Compass, or other applications.

MongoDB provides two formats for the connection string: a standard format and the DNS seed list format.

The standard format is used to connect to standalone clusters, replica sets, or sharded clusters.

The DNS Seed List format was released in MongoDB version 3.6.

It allows us to provide a DNS server list to our connection string. This gives us more flexibility of deployment, and the ability to change the servers in rotation without reconfiguring any of the clients.

Before we go into the components that make up a MongoDB connection string, let’s locate the connection string in our Atlas dashboard.

Make sure you’re logged into Atlas, and then navigate to the projects dashboard, which shows our project and cluster name.

Next, click the Connect button.

This will open a new window which lists three connection options:

Connect with the MongoDB Shell

Connect your application

Connect using MongoDB Compass

All three options share a similar connection string format, but the instructions for connecting vary.

Next, click “Connect your application” so that we can examine the connection string.

For now, I want to focus on the connection string that is provided here.

At first glance, the connection string appears to be a long string of characters. But we can actually break it down into separate components.

It begins with a required prefix, “mongodb,” that identifies it as a MongoDB connection string.

The connection string from the Atlas dashboard uses a DNS seed listentry which has a list of hosts behind it that we connect to.

The “+srv” addition automatically sets the TLS security option to true and tells MongoDB to use the DNS seed list.

Following the MongoDB prefix, there’s the username and password that we created for our database in the Atlas dashboard.

Next is the host and optional port number to our database. If the port number is not specified, MongoDB will use default to port 27017.port 27017 by default.

The final piece of the connection string contains any options that we want to include, such as: Connection timeout, TLS & SSL, Connection pooling, Read and write concerns

In this connection string, retryWrites is set to true. This tells MongoDB drivers to automatically retry certain write operations if they fail.

MongoDB has options for just about anything you need when it comes to connecting to your database.

To summarize, here’s what you learned in this video:

First, you learned what the MongoDB connection string is.

Then, you learned that there are two formats for connection strings in MongoDB: standard format and DNS seed list format.

Next, we located our connection string in Atlas.

Finally, we identified and defined the components of a connection string.