Question: How do you configure the connection string for MongoDB sharding?


MongoDB sharding is a method used to distribute data across multiple servers or clusters. It's a way to scale horizontally by partitioning data into smaller chunks or 'shards'. To connect to a sharded cluster in MongoDB, you need to use a specific format for your connection string. This connection string will include details about the mongos instances (query routers) that interface with client applications and direct operations to the appropriate shard.

Standard Connection String URI Format

The MongoDB connection string URI (Uniform Resource Identifier) format for a sharded cluster generally looks like this:

  • mongodb:// is the required prefix to identify that this is a string in the standard MongoDB URI format.
  • mongos1:port,mongos2:port,... are the hostnames and port numbers of the mongos instances. You can specify one or more mongos hosts separated by commas. Including multiple mongos instances increases high availability.
  • /database is the name of the database to connect to. If the database does not exist, MongoDB creates it when you first store data for that database.
  • option1=value1&option2=value2 are the connection options as key-value pairs. While many options are available, some commonly used ones include readPreference to set the read preference, replicaSet for the name of the replica set, and ssl=true to enable SSL connection.


Connecting to a sharded cluster with two mongos instances might look like this:


In this example:

  • and are the addresses and ports of the mongos query routers.
  • myDatabase is the database name.
  • The query includes options specifying the replica set name (myReplicaSet) and setting the read preference to the nearest node (readPreference=nearest).

Important Considerations

  • Ensure that all specified mongos instances are accessible from the client application's network environment.
  • Use DNS hostnames instead of IP addresses where possible to make future infrastructure changes easier.
  • The choice of read preference and other connection options can significantly impact your application's performance and resilience. Review the MongoDB documentation for guidance on these settings.

By correctly configuring the connection string, your application can efficiently communicate with a MongoDB sharded cluster, leveraging its scalability and availability features.

Was this content helpful?

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.