Open In App

Introduction to MongoDB connection URIs

Last Updated : 18 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.


Next Article
Article Tags :

Similar Reads