Connection String URI Format
This document describes the URI format for defining connections between applications using MongoDB drivers and MongoDB instances.
You can specify the MongoDB connection string using either:
This section describes the standard format of the MongoDB connection URI used to connect to a MongoDB deployment: standalone, replica set, or a sharded cluster.
The standard URI connection scheme has the form:
- Replica Set
- Sharded Cluster
For a replica set, specify the hostname of the
mongod instance as listed in the replica set configuration.
For a replica set, include the
For a connection string to a sharded cluster, specify
mongos hosts in the connection string.
For more examples, see Examples.
The standard URI connection string includes the following components:
||A required prefix to identify that this is a string in the standard connection format.|
Optional. Authentication credentials. If specified, the client will attempt to log in to the specific database using these credentials after connecting.
If the username or password includes the at sign
The host (and optional port number) where the
If the port number is not specified, the default port
||Optional. The name of the database to authenticate if the connection string includes authentication credentials in the form of
Optional. A query string that specifies connection specific options as
If the connection string does not specify a database/ you must specify a slash (
New in version 3.6.
In addition to the standard connection format, MongoDB supports a DNS-constructed seedlist. Using DNS to construct the available servers list allows more flexibility of deployment and the ability to change the servers in rotation without reconfiguring clients.
In order to leverage the DNS seedlist, use a connection string prefix of
mongodb+srv: in place of the
mongodb: string above.
+srv indicates to the mongo client that the hostname that follows corresponds to a DNS SRV record. The client driver will then query the DNS for the record to determine the hosts that are running the mongod instances.
For example, to connect to a DNS-listed hostname:
A typical DNS configuration for the connection string above might look something like this:
The hostnames returned in SRV records must share the same parent domain (in this example,
example.com) as the given hostname.
The DNS seedlist connection string can also provide options as a query string, with a trailing “/?” as in the standard connection string above. However, the
+srv appended to the standard connection string signals the driver to query the DNS for options as a configured TXT record.
Only two options are available for configuration via a TXT record –
authSource, and only one TXT record is allowed per server. If multiple TXT records appear in the DNS and/or if the TXT record contains an option other than
authSource, an error will be thrown by the driver.
An example of a properly configured TXT record:
In this case, taking into account both the DNS SRV records and the options retrieved from the TXT records, the parsed string will look like:
Options set in a TXT record can be overridden by passing in a query string with the URI. In the example below, the query string has provided an override for the
authSource option configured in the TXT record of the DNS entry above.
The rest of the option string will remain, and we can expect that the resulting URI would look like this (after parse).
mongodb+srv option will fail if there is no available DNS with records that correspond to the hostname identified in the connection string. In addition, use of the
+srv connection string modifier sets the
ssl option to
true automatically for the connection. This can be overridden by explicitly setting the
ssl option to
ssl=false in the query string.
This section lists all connection options used in the Standard Connection String Format.
Connection options are pairs in the following form:
value is always case sensitive. Separate options with the ampersand (i.e.
&) character. In the following example, a connection uses the
Semi-colon separator for connection string arguments
To provide backwards compatibility, drivers currently accept semi-colons (i.e.
;) as option separators.
The following connection string to a replica set named
myRepl with members running on the specified hosts:
When connecting to a replica set, provide a seed list of the replica set member(s) to the
The following connection string to a replica set includes
A boolean to enable or disables TLS/SSL for the connection:
|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.|
|The time in milliseconds to attempt a send or receive on a socket before the attempt times out. The default is never to timeout, though different drivers might vary. See the driver documentation.|
You can specify the following compressors:
If you specify multiple compressors, then the order in which you list the compressors matter as well as the communication initiator. For example, if the client specifies the following network compressors