Connection String Options
This page lists all connection option for both SRV connection strings and standard connection strings.
Connection options are pairs in the following form: name=value
.
The option
name
is case insensitive when using a driver.The option
name
is case insensitive when usingmongosh
.The
value
is always case sensitive.
Separate options with the ampersand (&
) character
name1=value1&name2=value2
. In the following example, a
connection includes the replicaSet
and
connectTimeoutMS
options:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net:27017,db2.example.net:2500/?replicaSet=test&connectTimeoutMS=300000
Note
Semi-colon separator for connection string arguments
To provide backwards compatibility, drivers currently accept
semi-colons (;
) as option separators.
Replica Set Option
The following connection string connects to a replica set named
myRepl
with members running on the specified hosts. It
authenticates as user myDatabaseUser
with the password
D1fficultP%40ssw0rd
:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db0.example.com:27017,db1.example.com:27017,db2.example.com:27017/?replicaSet=myRepl
Connection Option | Description |
---|---|
Specifies the name of the replica set, if the
When connecting to a replica set, provide a seed list of
the replica set member(s) to the | |
Specifies whether the client connects directly to the
IMPORTANT: When a replica set runs in Docker, it might expose only one
MongoDB endpoint. In this case, the replica set is not discoverable, and specifying
In a test or development environment, you can connect to the replica set by specifying
|
Connection Options
TLS Options
The following connection string to a replica set includes
tls=true
option. It authenticates as user myDatabaseUser
with the password D1fficultP%40ssw0rd
.
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db0.example.com,db1.example.com,db2.example.com/?replicaSet=myRepl&tls=true
Alternatively, you can also use the equivalent ssl=true
option:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db0.example.com,db1.example.com,db2.example.com/?replicaSet=myRepl&ssl=true
Connection Option | Description |
---|---|
Enables or disables TLS/SSL for the connection:
The If the | |
A boolean to enable or disable TLS/SSL for the connection:
The If the | |
Specifies the location of a local The client presents this file to the
This option is not supported by all drivers. Refer to the Drivers documentation. This connection string option is not available for the | |
Specifies the password to de-crypt the
This option is not supported by all drivers. Refer to the Drivers documentation. This connection string option is not available for the | |
Specifies the location of a local This option is not supported by all drivers. Refer to the Drivers documentation. This connection string option is not available for the | |
Bypasses validation of the certificates presented by the
Set to This option is not supported by all drivers. Refer to the Drivers documentation. This connection string option is not available for the WARNING: Disabling certificate validation creates a vulnerability. | |
Disables hostname validation of the certificate presented by
the Set to This option is not supported by all drivers. Refer to the Drivers documentation. This connection string option is not available for the WARNING: Disabling certificate validation creates a vulnerability. | |
Disables various certificate validations. Set to This connection string option is not available for the WARNING: Disabling certificate validation creates a vulnerability. |
Timeout Options
Connection Option | Description |
---|---|
The time in milliseconds to attempt a connection before timing out. The default is 10,000 milliseconds, but specific drivers might have a different default. For details, 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. |
Compression Options
Connection Option | Description | ||||||||
---|---|---|---|---|---|---|---|---|---|
Comma-delimited string of compressors to enable
network compression for communication between this
client and a 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 Messages are compressed when both parties enable network compression. Otherwise, messages between the parties are uncompressed. If the parties do not share at least one common compressor, messages between the parties are uncompressed.
| |||||||||
An integer that specifies the compression level if using
zlib for You can specify an integer value ranging from
Supported by |
Connection Pool Options
Most drivers implement some kind of connection pool handling. 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.
Connection Option | Description |
---|---|
The maximum number of connections in the connection pool. The
default value is | |
The minimum number of connections in the connection pool. The
default value is The | |
Maximum number of connections a pool may be establishing
concurrently. The default value is
Raising the value of | |
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 multiplies the This option is not supported by all drivers. | |
The maximum time in milliseconds that a thread can wait for a connection to become available. For default values, see the driver documentation. This option is not supported by all drivers. |
Write Concern Options
Write concern describes the level of acknowledgment requested from MongoDB. The write concern option is supported by the:
MongoDB drivers
You can specify the write concern both in the connection string and
as a parameter to methods like insert
or update
. If the
write concern is specified in both places, the method parameter
overrides the connection-string setting.
MongoDB Atlas deployment connection strings use "majority"
by default. If you don't specify write concern for an MongoDB Atlas
deployment, MongoDB Atlas enforces "majority"
.
The following connection string to a replica set specifies
"majority"
write concern and a 5 second
timeout using the wtimeoutMS
write concern parameter:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db0.example.com,db1.example.com,db2.example.com/?replicaSet=myRepl&w=majority&wtimeoutMS=5000
Connection Option | Description |
---|---|
Corresponds to the write concern You can specify a When If you set |
For more information, see Write Concern.
readConcern Options
For the WiredTiger storage engine, MongoDB introduces the
readConcern
option for replica sets and replica set shards.
Read Concern allows clients to choose a level of isolation for their reads from replica sets.
The following connection string to a replica set specifies
readConcernLevel=majority
:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db0.example.com,db1.example.com,db2.example.com/?replicaSet=myRepl&readConcernLevel=majority
Connection Option | Description | ||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
The level of isolation. Can accept one of the following values: This connection string option is not available for
Read Preference OptionsRead preferences describe the behavior of read operations with regards to replica sets. These parameters allow you to specify read preferences on a per-connection basis in the connection string. For example:
Order matters when using multiple
For more information, see Read preferences. Authentication OptionsThe following connection string to a replica set specifies the
If the username or password includes the following characters, those characters must be converted using percent encoding:
Server Selection and Discovery OptionsMongoDB provides the following options to configure how MongoDB drivers
and
Miscellaneous Configuration
On this page
Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!
|