This document describes the URI format for defining connections between applications and MongoDB instances in the official MongoDB drivers.
This section describes the standard format of the MongoDB connection URI used to connect to a MongoDB database server. The format is the same for all official MongoDB drivers. For a list of drivers and links to driver documentation, see MongoDB Drivers and Client Libraries.
The following is the standard URI connection scheme:
The components of this string are:
A required prefix to identify that this is a string in the standard connection format.
Optional. If specified, the client will attempt to log in to the specific database using these credentials after connecting to the mongod instance.
This the only required part of the URI. It identifies a server address to connect to. It identifies either a hostname, IP address, or UNIX domain socket.
Optional. The default value is :27017 if not specified.
Optional. You can specify as many hosts as necessary. You would specify multiple hosts, for example, for connections to replica sets.
Optional. The default value is :27017 if not specified.
Optional. The name of the database to authenticate if the connection string includes authentication credentials in the form of username:password@. If /database is not specified and the connection string includes credentials, the driver will authenticate to the admin database.
Connection specific options. See Connection String Options for a full description of these options.
If the connection string does not specify a database/ you must specify a slash (i.e. /) between the last hostN and the question mark that begins the string of options.
To describe a connection to a replica set named test, with the following mongod hosts:
You would use a connection string that resembles the following:
This section lists all connection options used in the Standard Connection String Format.The options are not case-sensitive.
Connection options are pairs in the following form: name=value. Separate options with the ampersand (i.e. &) character. In the following example, a connection uses the replicaSet and connectTimeoutMS options:
Semi-colon separator for connection string arguments
To provide backwards compatibility, drivers currently accept semi-colons (i.e. ;) as option separators.
When connecting to a replica set it is important to give a seed list of at least two mongod instances. If you only provide the connection point of a single mongod instance, and omit the replicaSet, the client will create a standalone connection.
true: Initiate the connection with SSL.
false: Initiate the connection without SSL.
The default value is false.
The time in milliseconds to attempt a connection before timing out. The default is never to timeout, though different drivers might vary. See the driver documentation.
Most drivers implement some kind of connection pooling handle this for you behind the scenes. Some drivers do not support connection pools. See your driver documentation for more information on the connection pooling implementation. These options allow applications to configure the connection pool when connecting to the MongoDB deployment.
The maximum number of connections in the connection pool. The default value is 100.
The minimum number of connections in the connection pool. The default value is 0.
The minPoolSize option is not supported by all drivers. For information on your driver, see the drivers documentation.
The maximum number of milliseconds that a connection can remain idle in the pool before being removed and closed.
This option is not supported by all drivers.
A number that the driver multiples the maxPoolSize value to, to provide the maximum number of threads allowed to wait for a connection to become available from the pool. For default values, see the MongoDB Drivers and Client Libraries documentation.
Write concern describes the kind of assurances that the program:mongod and the driver provide to the application regarding the success and durability of the write operation. For a full explanation of write concern and write operations in general see the: Write Operations:
Defines the level and kind of write concern, that the driver uses when calling getLastError. This option can take either a number or a string as a value.
The time in milliseconds to wait for replication to succeed, as specified in the w option, before timing out.
Specifies the replica set read preference for this connection. This setting overrides any slaveOk value. The read preference values are the following:
For descriptions of each value, see Read Preference Modes.
Specifies a tag set as a comma-separated list of colon-separated key-value pairs. For example:
To specify a list of tag sets, use multiple readPreferenceTags. The following specifies two tag sets and an empty tag set:
Order matters when using multiple readPreferenceTags.
For the default, see the drivers documentation for your driver.
Not all drivers support the uuidRepresentation option. For information on your driver, see the drivers documentation.
Consider the following example MongoDB URI strings, that specify common connections:
Connect to a database server running locally on the default port:
Connect and log in to the admin database as user sysop with the password moon:
Connect and log in to the records database as user sysop with the password moon:
Connect to a UNIX domain socket:
Not all drivers support UNIX domain sockets. For information on your driver, see the drivers documentation.
Connect to a replica set with two members, one on db1.example.net and the other on db2.example.net:
Connect to a replica set with three members running on localhost, on ports 27017, 27018, and 27019:
Connect to a replica set with write concern configured to wait for replication to succeed on at least two members, with a two-second timeout.