Docs Menu
Docs Home
/ / /
Java Sync
/ /

Connection Options

This section explains MongoDB connection and authentication options supported by the driver. You can pass the connection options as parameters of the connection URI to specify the behavior of the client.

Option Name
Type
Description
minPoolSize
integer

Specifies the minimum number of connections that must exist at any moment in a single connection pool.

Default: 0
maxPoolSize
integer

Specifies the maximum number of connections that a connection pool may have at a given time.

Default: 100
waitQueueTimeoutMS
integer

Specifies the maximum amount of time, in milliseconds that a thread may wait for a connection to become available.

Default: 120000 (120 seconds)
serverSelectionTimeoutMS
integer

Specifies the maximum amount of time, in milliseconds, the driver will wait for server selection to succeed before throwing an exception.

Default: 30000 (30 seconds)
localThresholdMS
integer

When communicating with multiple instances of MongoDB in a replica set, the driver will only send requests to a server whose response time is less than or equal to the server with the fastest response time plus the local threshold, in milliseconds.

Default: 15
heartbeatFrequencyMS
integer

Specifies the frequency, in milliseconds that the driver will wait between attempts to determine the current state of each server in the cluster.

Default: 10000 (10 seconds)
replicaSet
string

Specifies that the connection string provided includes multiple hosts. When specified, the driver attempts to find all members of that set.

Default: null
ssl
boolean

Specifies that all communication with MongoDB instances should use TLS/SSL. Superseded by the tls option.

Default: false
tls
boolean

Specifies that all communication with MongoDB instances should use TLS. Supersedes the ssl option.

Default: false
tlsInsecure
boolean

Specifies that the driver should allow invalid hostnames for TLS connections. Has the same effect as setting tlsAllowInvalidHostnames to true. To configure TLS security constraints in other ways, use a custom SSLContext.

Default: false
tlsAllowInvalidHostnames
boolean

Specifies that the driver should allow invalid hostnames in the certificate for TLS connections. Supersedes sslInvalidHostNameAllowed.

Default: false
connectTimeoutMS
integer

Specifies the maximum amount of time, in milliseconds, the Java driver waits for a connection to open before timing out. A value of 0 instructs the driver to never time out while waiting for a connection to open.

Default: 10000 (10 seconds)
socketTimeoutMS
integer

Specifies the maximum amount of time, in milliseconds, the Java driver will wait to send or receive a request before timing out. A value of 0 instructs the driver to never time out while waiting to send or receive a request.

Default: 0
maxIdleTimeMS
integer

Specifies the maximum amount of time, in milliseconds, that the driver allows a pooled connection to idle before closing the connection. A value of 0 indicates that there is no upper bound on how long the driver allows a pooled connection to be idle.

Default: 0
maxLifeTimeMS
integer

Specifies the maximum amount of time, in milliseconds, the Java driver will continue to use a pooled connection before closing the connection. A value of 0 indicates that there is no upper bound on how long the driver can keep a pooled connection open.

Default: 0
journal
boolean

Specifies that the driver must wait for the connected MongoDB instance to group commit to the journal file on disk for all writes.

Default: false
w
string or integer

Specifies the write concern. For more information on values, see the server documentation for the w option.

Default: 1
wtimeoutMS
integer

Specifies a time limit, in milliseconds, for the write concern. For more information, see the server documentation for the wtimeoutMS option. A value of 0 instructs the driver to never time out write operations.

Default: 0
readPreference
string

Specifies the read preference. For more information on values, see the server documentation for the readPreference option.

Default: primary
readPreferenceTags
string

Specifies the read preference tags. For more information on values, see the server documentation for the readPreferenceTags option.

Default: null
maxStalenessSeconds
integer

Specifies, in seconds, how stale a secondary can be before the driver stops communicating with that secondary. The minimum value is either 90 seconds or the heartbeat frequency plus 10 seconds, whichever is greater. For more information, see the server documentation for the maxStalenessSeconds option. Not providing a parameter or explicitly specifying -1 indicates that there should be no staleness check for secondaries.

Default: -1
authMechanism
string

Specifies the authentication mechanism that the driver should use if a credential was supplied.

Default: By default, the client picks the most secure mechanism available based on the server version. For possible values, see the server documentation for the authMechanism option.
authSource
string

Specifies the database that the supplied credentials should be validated against.

Default: admin
authMechanismProperties
string

Specifies authentication properties for the specified authentication mechanism as a list of colon-separated properties and values. For more information, see the server documentation for the authMechanismProperties option.

Default: null
appName
string

Specifies the name of the application provided to MongoDB instances during the connection handshake. Can be used for server logs and profiling.

Default: null
compressors
string

Specifies one or more compression algorithms that the driver will attempt to use to compress requests sent to the connected MongoDB instance. Possible values include: zlib, snappy, and zstd.

Default: null
zlibCompressionLevel
integer

Specifies the degree of compression that Zlib should use to decrease the size of requests to the connected MongoDB instance. The level can range from -1 to 9, with lower values compressing faster (but resulting in larger requests) and larger values compressing slower (but resulting in smaller requests).

Default: null
retryWrites
boolean

Specifies that the driver must retry supported write operations if they fail due to a network error.

Default: true
retryReads
boolean

Specifies that the driver must retry supported read operations if they fail due to a network error.

Default: true
uuidRepresentation
string

Specifies the UUID representation to use for read and write operations. For more information, see the driver documentation for the MongoClientSettings.getUuidRepresentation() method.

Default: unspecified
directConnection
boolean

Specifies that the driver must connect to the host directly.

Default: false
maxConnecting
integer

Specifies the maximum number of connections a pool may be establishing concurrently.

Default: 2
srvServiceName
string

Specifies the service name of the SRV resource records the driver retrieves to construct your seed list. You must use the DNS Seed List Connection Format in your connection URI to use this option.

Default: mongodb

For a complete list of options, see the ConnectionString API reference page.

Back

Connect to MongoDB