- Reference >
- Connection String URI Format
Connection String URI Format¶
This document describes the URI format for defining connections between applications and MongoDB instances in the official MongoDB drivers.
Standard Connection String Format¶
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 drivers.
The following is the standard URI connection scheme:
The components of this string are:
mongodb://A required prefix to identify that this is a string in the standard connection format.
username:password@Optional. If specified, the client will attempt to log in to the specific database using these credentials after connecting to the
mongodinstance.host1This 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.
:port1Optional. The default value is
:27017if not specified.hostXOptional. You can specify as many hosts as necessary. You would specify multiple hosts, for example, for connections to replica sets.
:portXOptional. The default value is
:27017if not specified./databaseOptional. The name of the database to authenticate if the connection string includes authentication credentials in the form of
username:password@. If/databaseis not specified and the connection string includes credentials, the driver will authenticate to theadmindatabase.?optionsConnection 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 lasthostNand the question mark that begins the string of options.
Example
To describe a connection to a replica set named test,
with the following mongod hosts:
db1.example.neton port27017anddb2.example.neton port2500.
You would use a connection string that resembles the following:
Connection String Options¶
This section lists all connection options used in the Standard Connection String Format.
Connection options are pairs in the following form: name=value. The
value is always case sensitive. 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.
Replica Set Option¶
-
uri.replicaSet¶ Specifies the name of the replica set, if the
mongodis a member of a replica set.When connecting to a replica set it is important to give a seed list of at least two
mongodinstances. If you only provide the connection point of a singlemongodinstance, and omit thereplicaSet, the client will create a standalone connection.
Connection Options¶
-
uri.ssl¶ true: Initiate the connection with TLS/SSL.false: Initiate the connection without TLS/SSL.The default value is
false.Note
The
ssloption is not supported by all drivers. See your driver documentation and the Configure mongod and mongos for TLS/SSL document.
Connection Pool Options¶
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.
-
uri.maxPoolSize¶ The maximum number of connections in the connection pool. The default value is
100.
-
uri.minPoolSize¶ The minimum number of connections in the connection pool. The default value is
0.Note
The
minPoolSizeoption is not supported by all drivers. For information on your driver, see the drivers documentation.
-
uri.maxIdleTimeMS¶ 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.
-
uri.waitQueueMultiple¶ A number that the driver multiples the
maxPoolSizevalue 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 drivers documentation.
Write Concern Options¶
Write concern describes the kind of assurances
that the 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
Write Concern Reference:
Note
You can specify the write concern both in the connection string and
as a parameter to method calls like insert or update. If the
write concern is specified in both places, the method parameter
overrides the connection-string setting.
-
uri.w¶ Corresponds to the write concern w Option. The
woption requests acknowledgement that the write operation has propagated to a specified number ofmongodinstances or tomongodinstances with specified tags.You can specify a
number, the stringmajority, or atag set.For details, see w Option.
-
uri.wtimeoutMS¶ Corresponds to the write concern wtimeout.
wtimeoutMSspecifies a time limit, in milliseconds, for the write concern.When
wtimeoutMSis0, write operations will never time out. For more information, see wtimeout.
-
uri.journal¶ Corresponds to the write concern j Option option. The
journaloption requests acknowledgement from MongoDB that the write operation has been written to the journal. For details, see j Option.If you set
journaltotrue, and specify awvalue less than 1,journalprevails.Changed in version 2.6: If you set
journalto true, and themongoddoes not have journaling enabled, as withstorage.journal.enabled, then MongoDB will error. In previous versions, MongoDB would provide basic receipt acknowledgment (i.e.w:1), ignorejournal, and include ajnotefield in its return document.
Read Preference Options¶
Read 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:
-
uri.readPreference¶ Specifies the replica set read preference for this connection. The read preference values are the following:
For descriptions of each value, see Read Preference Modes.
The default value is
primary, which sends all read operations to the replica set’s primary.
-
uri.readPreferenceTags¶ 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.
Authentication Options¶
-
uri.authSource¶ New in version 2.4.
Specify the database name associated with the user’s credentials, if the users collection do not exist in the database where the client is connecting.
authSourcedefaults to the database specified in the connection string.For authentication mechanisms that delegate credential storage to other services, the
authSourcevalue should be$externalas with thePLAIN(LDAP) andGSSAPI(Kerberos) authentication mechanisms.MongoDB will ignore
authSourcevalues if the connection string specifies no user name.
-
uri.authMechanism¶ New in version 2.4.
Changed in version 2.6: Added support for the
PLAINandMONGODB-X509authentication mechanisms.Changed in version 3.0: Added support for the
SCRAM-SHA-1authentication mechanism.Specify the authentication mechanism that MongoDB will use to authenticate the connection. Possible values include:
- SCRAM-SHA-1
- MONGODB-CR
- MONGODB-X509
- GSSAPI (Kerberos)
- PLAIN (LDAP SASL)
Only MongoDB Enterprise
mongodandmongosinstances provideGSSAPI(Kerberos) andPLAIN(LDAP) mechanisms. To useMONGODB-X509, you must have TLS/SSL Enabled.See Authentication for more information about the authentication system in MongoDB. Also consider Use x.509 Certificates to Authenticate Clients for more information on x509 authentication.
-
uri.gssapiServiceName¶ Set the Kerberos service name when connecting to Kerberized MongoDB instances. This value must match the service name set on MongoDB instances.
gssapiServiceNamedefaults tomongodbfor all clients and for MongoDB instance. If you changesaslServiceNamesetting on a MongoDB instance, you will need to setgssapiServiceNameto the same value.
Miscellaneous Configuration¶
-
uri.uuidRepresentation¶ Option Description standardThe standard binary representation. csharpLegacyThe default representation for the C# driver. javaLegacyThe default representation for the Java driver. pythonLegacyThe default representation for the Python driver. For the default, see the drivers documentation for your driver.
Note
Not all drivers support the
uuidRepresentationoption. For information on your driver, see the drivers documentation.
Examples¶
The following provide example URI strings for common connection targets.
Database Server Running Locally¶
The following connects to a database server running locally on the default port:
admin Database¶
The following connects and logs in to the admin database as user
sysop with the password moon:
records Database¶
The following connects and logs in to the records database as user
sysop with the password moon:
UNIX Domain Socket¶
The following connects to a UNIX domain socket:
Note
Not all drivers support UNIX domain sockets. For information on your driver, see the drivers documentation.
Replica Set with Members on Different Machines¶
The following connects to a replica set with two members, one on
db1.example.net and the other on db2.example.net:
Replica Set with Members on localhost¶
The following connects to a replica set with three members running on localhost on
ports 27017, 27018, and 27019:
Replica Set with Read Distribution¶
The following connects to a replica set with three members and distributes reads to the secondaries:
Replica Set with a High Level of Write Concern¶
The following connects to a replica set with write concern configured to wait for replication to succeed on at least two members, with a two-second timeout.