MongoDB connection string

If you’ve been shopping around for a MongoDB client, you may have come across the cumbersome task of manually entering connection details, or writing out a MongoDB connection string in the correct URI format.

Class ConnectionString
java.lang.Object
com.mongodb.ConnectionString
public class ConnectionString
extends Object

Represents a Connection String. The Connection String describes the hosts to be used and options. Learn more from MongoDB Training

The format of the Connection String is:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database.collection][?options]]
  • mongodb:// is a required prefix to identify that this is a string in the standard connection format.
  • username:password@ are optional. If given, the driver will attempt to login to a database after connecting to a database server. For some authentication mechanisms, only the username is specified and the password is not, in which case the “:” after the username is left off as well
  • host1 is the only required part of the connection string. It identifies a server address to connect to. Support for Unix domain sockets was added in 3.7. Note: The path must be urlencoded eg: mongodb://%2Ftmp%2Fmongodb-27017.sock and the jnr.unixsocket library installed.
  • :portX is optional and defaults to :27017 if not provided.
  • /database is the name of the database to login to and thus is only relevant if the username:password@ syntax is used. If not specified the “admin” database will be used by default.
  • ?options are connection options. Note that if database is absent there is still a / required between the last host and the ? introducing the options. Options are name=value pairs and the pairs are separated by “&”. For backwards compatibility, “;” is accepted as a separator in addition to “&”, but should be considered as deprecated. Learn more practical skills from MongoDB Online Course

An alternative format, using the mongodb+srv protocol, is:

mongodb+srv://[username:password@]host[/[database][?options]]

Server Selection Configuration:

  • serverSelectionTimeoutMS=ms: How long the driver will wait for server selection to succeed before throwing an exception.
  • localThresholdMS=ms: When choosing among multiple MongoDB servers to send a request, the driver will only send that request to a server whose ping time is less than or equal to the server with the fastest ping time plus the local threshold.

Server Monitoring Configuration:

  • heartbeatFrequencyMS=ms: The frequency that the driver will attempt to determine the current state of each server in the cluster.

Replica set configuration:

  • replicaSet=name: Implies that the hosts given are a seed list, and the driver will attempt to find all members of the set.

Connection Configuration:

  • streamType=nio2|netty: The stream type to use for connections. If unspecified, nio2 will be used.
  • ssl=true|false: Whether to connect using SSL.
  • sslInvalidHostNameAllowed=true|false: Whether to allow invalid host names for SSL connections.
  • connectTimeoutMS=ms: How long a connection can take to be opened before timing out.
  • socketTimeoutMS=ms: How long a send or receive on a socket can take before timing out.
  • maxIdleTimeMS=ms: Maximum idle time of a pooled connection. A connection that exceeds this limit will be closed
  • maxLifeTimeMS=ms: Maximum life time of a pooled connection. A connection that exceeds this limit will be closed

For more in-depth knowledge, enroll for a live free demo on MongoDB Online Training

Leave a comment

Design a site like this with WordPress.com
Get started