For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Cluster Settings

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

serverSelectionTimeoutMS

integer

Specifies the maximum amount of time, in milliseconds, the driver waits 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 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: 15

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

directConnection

boolean

Specifies that the driver must connect to the host directly. This maps to applying mode(ClusterConnectionMode.SINGLE) to your MongoClientSettings.

Default: false

loadBalanced

boolean

Specifies whether or not the driver is connecting to MongoDB using a load balancer.

Default: null

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

srvAllowedHostsSuffix

string

Specifies the domain suffix that the driver uses to validate the host names returned by the SRV record lookup. When you set this option, the driver validates host names against the specified suffix instead of the domain inferred from the SRV host name. If the value doesn't begin with a period (.), the driver adds one.

You can use this option only with the mongodb+srv connection string format.

Default: null. The driver validates host names against the domain inferred from the SRV host name.

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)

Warning

Changing the default SRV host name validation can create security vulnerabilities, because it relaxes a built-in safeguard against DNS spoofing. Specify the narrowest suffix that covers the hosts in your deployment. For example, if your SRV host name is cluster.test.internal.example.com, set srvAllowedHostsSuffix to .internal.example.com. Do not set it to .example.com, which accepts any host in the example.com domain.

For more information about these parameters, see the ConnectionString API documentation.

Rate this page

On this page