Cluster Settings
On this page
Overview
In this guide, you can learn about how the Java driver manages clusters.
You can specify settings for your clusters by using either a connection
string or by passing a MongoClientSettings
object to the
MongoClient constructor. Select the Connection
String or MongoClientSettings tab to see the options available:
Include the following parameters in your connection string to modify the driver's behavior when interacting with your MongoDB cluster:
Option Name | Type | Description |
---|---|---|
| integer | Specifies the maximum amount of time, in milliseconds, the driver waits for server selection to succeed before throwing an exception. Default: |
| integer | When communicating with multiple instances of MongoDB in a replica set, the driver sends requests only 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: |
| string | Specifies that the connection string provided includes multiple hosts. When specified, the driver attempts to find all members of that set. Default: |
| boolean | Specifies that the driver must connect to the host directly. This
maps to applying Default: |
| 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: |
This example connects the driver directly to a server, regardless of the type of MongoDB cluster it's a part of:
ConnectionString connectionString = "mongodb://<host>:<port>/?directConnection=true" MongoClient mongoClient = MongoClients.create(connectionString)
For more information about these parameters, see the ConnectionString API documentation.
Chain the applyToClusterSettings() method to modify the driver's behavior when interacting with your MongoDB cluster.
The following table describes the methods you can chain to your settings to modify the driver's behavior:
Method | Description | |||
---|---|---|---|---|
| Adds a listener for cluster-related events. | |||
| Uses the settings from a | |||
| Uses the cluster settings specified in a | |||
| Sets all the specified locations of a Mongo deployment. | |||
| Sets the amount of time that a server’s round trip can take and still be eligible for server selection. Default: | |||
| Sets how to connect to a MongoDB deployment. | |||
| Sets the type of cluster required for the cluster. | |||
| Sets the replica set name required for the cluster. | |||
| Sets the maximum time to select a primary node before throwing a timeout exception. Default: | |||
| Adds a server selector to apply before server selection. | |||
| Sets the host name to use to look up an SRV DNS record to find the MongoDB hosts. If you want to enable the processing of TXT records associated with the host, specify the SRV host in the connection string using the For example:
| |||
| Sets the maximum number of hosts the driver can connect to when
using the DNS seedlist (SRV) connection protocol, identified by
the Throws an exception if you are not using the SRV connection protocol. |
This example connects the driver directly to a server, regardless of the type of MongoDB cluster it's a part of:
MongoClient mongoClient = MongoClients.create( MongoClientSettings.builder() .applyToClusterSettings(builder -> builder.mode(ClusterConnectionMode.SINGLE)) .build());
Tip
This is analogous to the directConnection
parameter you can specify
in your connection URI. See the Connection String tab for more
information.
For more information about the chained methods, see the MongoClientSettings.Builder API documentation.