Overview
This section describes the MongoDB connection and authentication options you can pass in as parameters of the connection URI in the Java Reactive Streams driver.
Using the Connection URI
If you pass a connection URI to the MongoClient constructor, you can include connection options in the string as <name>=<value> pairs. In the following example, the connection URI contains the connectTimeoutMS option with a value of 60000 and the tls option with a value of true:
public class MongoConnection { public static void main(String[] args) { String uri = "mongodb://<hostname>:<port>/?connectTimeoutMS=60000&tls=true"; try (MongoClient client = MongoClients.create(uri)) { // Use client here } } }
Connection Options
The following sections describe the connection options available in the Java Reactive Streams driver. You can pass these options in as parameters of the connection URI to specify the behavior of the client.
Authentication
Connection Option | Description |
|---|---|
authMechanism | Sets the mechanism the Java Reactive Streams driver uses to authenticate the |
authMechanismProperties | Sets options specific to the authentication mechanism. |
authSource | Sets the source of authentication credentials. The source is typically a |
username | Sets the username for authentication. When this option is included in a connection |
password | Sets the password for authentication. When this option is included in a connection |
Connection Configuration
Connection Option | Description |
|---|---|
connectTimeoutMS | Sets the number of milliseconds the connection can take before timing out. If an |
maxIdleTimeMS | Sets the maximum number of milliseconds that a connection can |
maxLifeTimeMS | Sets the maximum lifetime in milliseconds of a pooled connection. If an |
socketTimeoutMS (deprecated) | This option is deprecated. You can configure this timeout by |
ssl | Sets whether to connect using TLS/SSL. |
sslInvalidHostNameAllowed | Allows invalid host names for TLS/SSL connections. |
tls | Sets whether to connect using TLS. Supersedes the |
tlsAllowInvalidHostnames | Allows invalid host names for TLS connections. Supersedes the |
tlsInsecure | Enables insecure TLS connections when connecting using TLS. |
Connection Pool Configuration
Connection Option | Description |
|---|---|
maxConnecting | Sets the maximum number of connections a pool can establish concurrently. |
maxPoolSize | Sets the maximum number of connections in the connection pool.
If an operation needs a new connection while the connection pool has
|
minPoolSize | Sets the minimum number of connections in the connection pool. |
waitQueueTimeoutMS (deprecated) | This option is deprecated. You can configure this timeout by |
Compressor Configuration
Connection Option | Description |
|---|---|
compressors | Sets preferred compression types, in order, for wire-protocol messages sent to |
zlibCompressionLevel | Sets the compression level for zlib to use. This option accepts |
General Configuration
Connection Option | Description |
|---|---|
directConnection | Sets whether the driver connects directly to the host. |
loadBalanced | Sets whether or not the driver is connecting to MongoDB using a load |
retryWrites | Sets whether or not the driver retries supported write operations if |
retryReads | Sets whether or not the driver retries supported read operations if |
uuidRepresentation | Sets the mechanism the Java Reactive Streams driver uses to encode instances of |
Proxy Configuration
Connection Option | Description |
|---|---|
proxyHost | Establishes the SOCKS5 proxy host to establish a connection through. |
proxyPort | Sets the port number for the SOCKS5 proxy server. Must be a non-negative integer. |
proxyUsername | Sets the username for authenticating with the proxy server. Required if |
proxyPassword | Sets the password for authenticating with the proxy server. Required if |
Read Preference Configuration
Connection Option | Description |
|---|---|
readPreference | Sets the read preference for this connection. Enumerated values are: |
readPreferenceTags | Specifies a tag set as a comma-separated list of |
maxStalenessSeconds | Sets the maximum staleness in seconds. To learn more about staleness, |
Replica Set Configuration
Connection Option | Description |
|---|---|
replicaSet | Specifies the name of the replica set to connect to. |
Server Configuration
Connection Option | Description |
|---|---|
appName | Sets the logical name of the application. |
heartbeatFrequencyMS | The frequency in milliseconds of the driver determining the current state of each server in the cluster. |
localThresholdMS | Sets a ping time threshold for choosing among multiple MongoDB servers |
serverMonitoringMode | Sets the server monitoring mode, which defines the monitoring
protocol to use. The enumerated values are: |
serverSelectionTimeoutMS | Sets how long the driver will wait for server selection to succeed before throwing an exception. |
SRV Configuration
Connection Option | Description |
|---|---|
srvServiceName | Sets the SRV service name. See |
srvMaxHosts | Sets the maximum number of hosts from the SRV record to connect to. |
Write Concern Configuration
Connection Option | Description |
|---|---|
safe | Sets whether the driver ensures that all writes are acknowledged by the |
journal | Sets whether the driver waits for the server to group commit to the |
w | If set, the driver adds |
wtimeoutMS (deprecated) | This option is deprecated. You can configure this timeout by |
For more information about the connection options in this section, see the Databases and Collections guide.