How to Get the Database URL in MongoDB
Last Updated :
23 Jul, 2025
When working with MongoDB, it is important to know how to retrieve the database URL because as it enables applications to connect to the database.
In this article, we'll explore how to obtain the database URL in MongoDB by covering various scenarios and providing examples.
How to Get the Database URL in MongoDB
Below are the approaches that can help us to Get the Database URL in MongoDB are as follows:
- Connection String from MongoDB Atlas
- Connection String from Local MongoDB Instance
- Connection String from Configuration File
Structure of MongoDB Connection URI:
A typical MongoDB connection URI follows the format
mongodb://username:password@host1:port1,host2:port2/database?option1=value1&option2=value2
Explanation:
- mongodb://: The protocol prefix indicating that the URI is for a MongoDB connection.
- username:password: Optional authentication credentials. If authentication is not required, this part can be omitted.
- @host1:port1,host2:port2: Comma-separated list of MongoDB hosts and ports. MongoDB supports connecting to replica sets or sharded clusters, so multiple hosts and ports can be specified.
- /database: The name of the MongoDB database to connect to.
- ?option1=value1&option2=value2: Optional connection options specified as query parameters.
1. Connection String from MongoDB Atlas
If we are using MongoDB Atlas, a cloud-hosted MongoDB service, the connection string can be obtained from the Atlas dashboard.
- Log in to your MongoDB Atlas account.
- Navigate to the "Clusters" section and select your cluster.
- Click on the "Connect" button.
- Choose "Connect Your Application".
- Copy the connection string provided.
Example Connection String from MongoDB Atlas:
mongodb+srv://
username:[email protected]/mydatabase?retryWrites=true&w=majority
2. Connection String from Local MongoDB Instance
If we have a local MongoDB instance running on your machine, you can obtain the connection string by following these steps:
- Open a terminal or command prompt.
- Start the MongoDB server if it's not already running.
- Run the `mongo` command to open the MongoDB shell.
- Run the `db` command to switch to a specific database (optional).
- Run the `db.getMongo()` command to get the connection string.
Output:
{
"ismaster" : true,
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2022-04-06T08:53:47.533Z"),
"logicalSessionTimeoutMinutes" : 30,
"connectionId" : 17,
"minWireVersion" : 0,
"maxWireVersion" : 14,
"readOnly" : false,
"ok" : 1
}The connection string can be extracted from the me field.
3. Connection String from Configuration File
If we are using a configuration file to store database settings, the connection string can be specified in the configuration file.
Example config.yml File:
mongodb:
url: mongodb://username:password@localhost:27017/mydatabase
Conclusion
Overall, Retrieving the database URL in MongoDB is straightforward whether you are using MongoDB Atlas, a local instance, or a configuration file. Understanding the structure of the MongoDB connection URI helps in forming the correct connection string for your application, ensuring seamless database connectivity.
Explore
Introduction
Installation
Basics of MongoDB
MongoDB Methods
Comparison Operators
Logical Operators
Arithmetic Operators
Field Update Operators
Array Expression Operators
Array Update Operators