After installing MongoDB for the first time, or spinning up a new cluster on MongoDB Atlas, one of the first challenges you may face is how to connect to your database so that you can start entering and querying data. To connect to your MongoDB cluster, you'll need a connection string.
A MongoDB connection string is a short piece of text that tells a MongoDB client how to connect to your MongoDB cluster. The URL can be provided to various tools, like MongoDB Compass and the MongoDB Shell, or it can be used in your own software built on one of the MongoDB drivers to specify a MongoDB cluster.
The URL can contain various information, such as the servers that make up the cluster (or how to find out that information), your database username and password, and the name of a default database to use for running queries, unless that's overridden in some other way.
There are three types of MongoDB connection strings that you'll encounter: A MongoDB Atlas connection string, a connection string for an instance running on your own computer (localhost), and the connection string for a self-hosted cluster.
We'll explain how to obtain each of these connection strings below.
In the MongoDB Atlas web interface, find the cluster to connect to, and click on the "Connect" button next to its name. In the next screen, select the method you're using to connect to MongoDB, and you'll be provided with a suitable connection string for your cluster. There's a small “copy-paste” button next to the connection string to make your life even easier!
You can find more information about MongoDB Atlas connection strings in the MongoDB documentation.
Be aware that if you choose the MongoDB Shell option, you'll be provided with the whole command-line for starting mongosh, and not just the connection string.
The connection string won't be complete. There will usually be placeholders for your username and password, surrounded by angle-brackets, like this:
mongodb+srv://<username>:<password>@beyondthebasics.abcde.mongodb.net/test
You'll need to find the username and password for your database user, or create a new database user to obtain these credentials. You can do that using the "Database Access" link on the left-hand side of the Atlas site, and clicking the "Add New Database User" button.
The MongoDB connection string for a local server would typically take the form of mongodb://localhost:27017/<database>
, where <database>
is the name of the database you want to connect to. If you are using a different port for MongoDB, you should replace 27017
with the port number you are using.
To get the connection string for a self-hosted MongoDB replica set, you will need to know the hostnames or IP addresses of the servers in the replica set, as well as the name of the replica set.
You can specify the individual servers in the replica set by including them in the connection string, like this:
mongodb://<host1>:<port1>,<host2>:<port2>,<host3>:<port3>/<database>?replicaSet=<replicaSetName>
If we had a replica set called repl1
, consisting of three hosts, 192.168.10.1-3
, and we were running them all on the default port, 27017
, our connection string would look like this:
mongodb://192.168.1.1:27017,192.168.1.2:27017,192.168.1.3:27017/<database>?replicaSet=repl1
An SRV connection string is a type of connection that starts with the prefix mongodb+srv://
(instead of just mongodb://
). All MongoDB Atlas connection strings are SRV connection strings — it makes them shorter and easier to use. The SRV protocol allows for automatic discovery of the servers in a MongoDB replica set or sharded cluster, without the need to specify the individual server addresses.
The technical details of exactly how SRV connection strings work is a little out of scope. Fortunately, we’ve got a blog post — Here to SRV You with Easier Replica Set Connections — if you want to learn all about them.
To connect MongoDB using a uniform resource identifier (URI), you need to provide a connection string that includes the host name, port, database name, and authentication credentials.
To set up a MongoDB connection, you need to install the MongoDB driver for your programming language, create a connection string with the appropriate parameters, and use the driver's connect method to establish a connection to the MongoDB server.
To connect to MongoDB, retrieve the hostname and port information from Cloud Manager and use a MongoDB client, such as mongosh or a MongoDB driver, then use the db.collection.find() method in the MongoDB Shell to query documents in a collection.