MongoDB

Forming MongoDB Connection String

The connection string is created when connecting to the MongoDB database server. The connection string in MongoDB is formed using the “mongodb” command. With the “mongodb” command, we can connect with the database server of MongoDB. Then, the hostname or IP address options are assigned to the mongodb command to establish the connection with the database server. Many other options are used to form the mongodb connection string which will be demonstrated in the following sections.

Example 1: Forming the MongoDB Connection String in MongoDB Shell

When we launch the MongoDB shell in our system, it asks to enter the connection string with the MongoDB server. The default string connection is also provided in the following shell as “mongodb://localhost/”. When we press enter without specifying any connection string, it automatically creates a direct connection with the database server of MongoDB. Note that in the following direct connection string, the mongodb command is given along with the “127.0.0.1” IP address and the port number is set as “27017”. Then, the directConnection parameter has the “true” Boolean value because the connection with the MongoDB server has been established. The “serverSelectionTimeoutMS” is “2000” which is the time set for waiting while choosing a server before throwing an exception. The next option is the name of the app which we use to establish a connection string in MongoDB.

mongodb://localhost:27017/

We can see that the direct connection string which is provided by the shell itself is connected to the MongoDB server. The “test” database is the default database name in the MongoDB shell.

Example 2: Forming the MongoDB Connection String in MongoDB Compass

This an example of a direct connection with the MongoDB database server where a default string connection is generated. We can also create the connection string by setting the parameters with the mongoDB in the previous shell. But we have the most convenient way of creating the connection string of MongoDB using the application of MongoDB compass. Here, open the MongoDB compass application where the “New Connection” page is opened. There, we have a “URI” section where the default connection string is already assigned. We also have access to edit the connection string of the “URI” section.  The default IP address and port number are in the following connection string:

mongodb://localhost:27017/

Example 3: Forming the MongoDB Connection String in MongoDB Compass for DNS Seed List

To use the DNS seed list, replace the “mongodb” default prefix in the MongoDB connection string with mongodb+srv or by choosing the mongodb+srv option from the MongoDB compass “Advance compass connection” section. Choosing that command automatically modifies the “URI” section where the connection string is placed. The +srv prefix denotes a DNS SRV record for the hostname. Next, the mongosh shell performs a DNS query to determine which hosts are running the mongod instances. The “URI” section in the following image is set with the connection string which uses the mongodb+srv command with the hostname as “localhost”.

mongodb+srv://localhost/

Example 4: Forming the MongoDB Connection String in MongoDB Compass with the Username/Password Parameters

The connection string of MongoDB is also set with the username and password to get the authentication with the MongoDB database server. Here, we can see the “Authentication” option inside the “Advance compass connection” section. The “Authentication” area provides the username and the password options which we set for the connection string. Both the username and password are “admin” in this case. Note that the connection string in the URI section is also updated with the username and password. The assigned username and password are automatically compared by MongoDB with the encrypted Hash that is kept in the backend.

mongodb://admin:admin@localhost:27017/?authMechanism=DEFAULT

As in the previous connection string, the authentication parameter – “authMechanism” – is set as default. We can also update the authentication mechanism with the encryption methods which are provided below the “Authentication mechanism” category.

Example 5: Forming the MongoDB Connection String in MongoDB Compass for Replica Set Name

The replication improves data accessibility and serves as a backup server in the case where the main server malfunctions. We can set the replication by giving the name of the replication for the MongoDB database connection with the “replicaSet” parameter. We can see in the following image that the replica set name option is filled with the replication name as “MySet” and the read preference field is set as “Primary”. Now, the connection string is updated with the “replicaSet” option with the name assigned to it.

mongodb://admin:admin@localhost:27017/?authMechanism=DEFAULT&tls=true&replicaSet=%3CMyReplSet%3E&connectTimeoutMS=600000&read
Preference=primary

Example 6: Forming the MongoDB Connection String in MongoDB Compass with the Shared Cluster

All the previous examples locally form the MongoDB connection string. We can use the MongoDB Atlas for free cloud-hosted forming of the connection strings. We need to open the cluster; we established it in the MongoDB Atlas. Then, a few options are provided at the top which is shown in the following screenshot. We have to click the “Connect” option.

When the “Connect” option is clicked, it generates the pop-up where we set up the connection for security purposes. First, we have to add an IP address connection. Then, we fulfill the authentication requirements.

Here, we choose a default IP address connection which is added by selecting the “Add your Current IP address”. Then, scroll over to the “Add IP address” at the right-end corner and submit it to save the IP address. Then, we have the “Create a database User” where we fulfill the “Username” field and the “Password” field. After that, submit the “Choose a connection method” option for the next step to accomplish.

We can choose any connection methods which are compatible and available in our system. Here, we select the third option which is to connect using the MongoDB compass.

Once the connection method is submitted, the connection string is generated for the application that we are using. The last field has the connection string which is specified in the “URI” section for the MongoDB connection deployment.

mongosh "mongodb+srv://cluster0.dq5d8.mongodb.net/myFirstDatabase" --apiVersion 1 --username admin

Conclusion

The MongoDB connection string is an important aspect to establish a connection with the server database. The connection string is where the MongoDB receives the majority of its configuration options. We first manually explored the creation of a connection string within the MongoDB shell and MongoDB compass. The MongoDB compass is the GUI approach to form the connection string in MongoDB. Furthermore, we defined a way to connect through a cloud connection string with the MongoDB server.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.