Introduction to MongoDB connection URIs
Last Updated :
18 Feb, 2025
MongoDB is a popular NoSQL database known for its scalability, flexibility, and ease of use. To connect applications to a MongoDB database, developers use MongoDB connection URIs. These URIs specify how clients should connect to a MongoDB instance, including details like authentication credentials, database names, and connection options.
In this article, we will explore MongoDB connection URIs in detail, including their structure, authentication methods, and common connection options.
Understanding MongoDB Connection URIs
A MongoDB connection URI is a string that provides all the necessary details for a client to establish a connection with a MongoDB database. The standard format for a MongoDB URI is as follows:
mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]
Or, for a TLS/SSL-enabled connection:
mongodb+srv://[username:password@]host[/[defaultauthdb][?options]]
Breaking Down the Connection URI
Each part of the connection URI plays a specific role:
1. Scheme (mongodb:// or mongodb+srv://)
mongodb:// → Standard connection string for standalone, replica set, or sharded clusters.
mongodb+srv:// → Used for automatic discovery of MongoDB cluster nodes through DNS.
2. Authentication Credentials (username:password@) (Optional)
- If authentication is required, credentials must be provided.
- Example: mongodb://admin:password@localhost:27017
3. Host and Port (host:port)
- The hostname (or IP address) and port of the MongoDB instance.
- Example: mongodb://localhost:27017 (Default port: 27017)
- Multiple hosts can be specified for replica sets or sharded clusters:
mongodb://host1:27017,host2:27018,host3:27019
4. Default Authentication Database (/defaultauthdb) (Optional)
- Defines the database where user credentials are stored.
- Example: mongodb://admin:password@localhost:27017/admin
5. Connection Options (?options) (Optional)
Allows additional configurations like timeouts, read preferences, write concerns, and authentication mechanisms.
Example:
mongodb://localhost:27017/?retryWrites=true&w=majority
Examples of MongoDB Connection URIs
1. Connecting to a Local MongoDB Instance
mongodb://localhost:27017
Connects to a MongoDB instance running on localhost at port 27017.
2. Connecting with Authentication
mongodb://user:password@localhost:27017/mydatabase
Uses user and password to authenticate against the mydatabase database.
3. Connecting to a Replica Set
mongodb://node1:27017,node2:27017,node3:27017/?replicaSet=myReplicaSet
Connects to a replica set named myReplicaSet.
4. Connecting to a MongoDB Atlas Cluster (SRV Protocol)
mongodb+srv://user:[email protected]/mydatabase?retryWrites=true&w=majority
Uses MongoDB Atlas DNS SRV records to automatically resolve cluster nodes.
Best Practices for Using MongoDB Connection URIs
1. Use Environment Variables
Store credentials securely using .env files or secret management tools.
Example in Node.js:
const uri = process.env.MONGO_URI;
Use the mongodb+srv:// Format for MongoDB Atlas
Simplifies cluster configuration using DNS SRV records.
2. Enable TLS/SSL for Secure Connections
- Use tls=true to encrypt data transmission.
- Specify Write Concern (w) and Read Preference (readPreference)
- Ensures reliable reads and writes in distributed environments.
3. Use Connection Pooling for Performance
Example in Node.js (Mongoose):
mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true, poolSize: 10 });
Conclusion
MongoDB connection URIs are essential for establishing connections between clients and MongoDB databases. They provide flexibility with authentication, replica sets, and additional connection options to optimize performance and security. Understanding how to construct and customize connection URIs can help developers effectively integrate MongoDB into their applications.
By following best practices, such as using environment variables, enabling SSL/TLS, and leveraging connection pooling, developers can ensure reliable and secure connections to MongoDB databases.
Similar Reads
MongoDB: An introduction
MongoDB is a powerful, open-source NoSQL database that offers a document-oriented data model, providing a flexible alternative to traditional relational databases. Unlike SQL databases, MongoDB stores data in BSON format, which is similar to JSON, enabling efficient and scalable data storage and ret
5 min read
How to use MongoDB Connection String
MongoDB connection strings are essential for establishing connections between applications and MongoDB databases. These strings contain crucial information such as server addresses, authentication credentials and optional parameters, enabling seamless communication. Understanding the structure and c
6 min read
Introduction about Node.js and MongoDB
NoSQL databases are databases that store and retrieve the data that is present in a non-tabular format. Depending on the format of the data stored, NoSQL databases are split into 4 major types: Key-Value Graph database Document-oriented Column family These databases are built to address the shortcom
4 min read
Mongoose Connection.prototype.get() Function
The Mongoose Schema API Connection.prototype.get() method of the Mongoose API is used on the Connection object. It allows us to get the value of the option properties. Using this method we can fetch the values of different properties from options object. Let us understand get() method using an examp
2 min read
Multiple MongoDB database Connections in NodeJS
In this guide, we will walk through the process of setting up a Node.js application that connects to multiple MongoDB databases using Mongoose. This is useful in scenarios where you need to manage different datasets separately, such as handling user data in one database and order data in another. Pr
5 min read
Encrypt Communication (TLS/SSL) in MongoDB
TLS (Transport Layer Security) and SSL (Secure Sockets Layer) are cryptographic protocols used to secure communication over a computer network. They encrypt data transmitted between a client and a server protecting it from unauthorized access. MongoDB a popular NoSQL database supports TLS/SSL to ens
6 min read
What is a collection in MongoDB?
MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for the storage and retrieval of data. Thi
4 min read
How to drop collection in MongoDb using Node.js ?
MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This fo
2 min read
Connecting MongoDB to Jupyter Notebook
MongoDB is one of the most famous NoSql databases. It is an open-source database. The simple meaning of the NoSql database is a non-relational database. i.e., It doesn't include the specific structure of databases such as tables and all like the relational database. So, you can design the schemas wi
3 min read
Mongoose Schema Connection.prototype.model() Function
The Mongoose Schema API Connection.prototype.model() of the Mongoose API is used on the Connection objects. It allows us to define new model in the MongoDB database for particular connection object. It is used to define and retrieve models in and from the database. Let us understand models() method
3 min read