Docs Menu
Docs Home
/ / /
Go Driver
/ /

Customize Cluster Settings

In this guide, you can learn how the Go driver manages clusters and how to customize the cluster settings.

You can specify settings for your clusters by using either a connection string or a ClientOptions struct when creating a new Client instance. Select the Connection String or ClientOptions tab to see the available options.

The following table describes the parameters you can use in your connection string to modify the driver's behavior when interacting with your MongoDB cluster:

Parameter
Description

serverSelectionTimeoutMS

Specifies the maximum amount of time the driver will wait for a server to be available before throwing an error.

Default: 30 seconds

localThresholdMS

Specifies the maximum latency in milliseconds for selecting a server.

Default: 15 milliseconds

replicaSet

Specifies the name of the replica set to connect to.

directConnection

Specifies whether to connect directly to a single server, bypassing the replica set or sharded cluster.

Default: false

loadBalanced

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

Default: false

srvServiceName

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 string to use this option.

The following table describes several methods you can chain to your ClientOptions struct to modify the driver's behavior:

Method
Description

SetServerSelectionTimeout()

Specifies the maximum amount of time the driver will wait for a server to be available before throwing an error.

Default: 30 seconds

SetLocalThreshold()

Specifies the maximum latency in milliseconds for selecting a server.

Default: 15 milliseconds

SetReplicaSet()

Specifies the name of the replica set to connect to.

SetDirect()

Specifies whether to connect directly to a single server, bypassing the replica set or sharded cluster.

Default: false

SetLoadBalanced()

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

Default: false

SetSRVServiceName()

Specifies a custom service name of the SRV resource records the driver retrieves to construct your seed list. To use a custom SRV service name in SRV discovery, you must call this function before you call ApplyURI().

To learn more about the available methods, see the API Documentation section.

Select the Connection String or ClientOptions tab to see the corresponding example:

The following code uses the connection string to configure the maximum server selection timeout to 10 seconds and the local threshold to 20 milliseconds:

const uri = "mongodb://localhost:27017/?serverSelectionTimeoutMS=10000&localThresholdMS=20"

The following code creates a client and passes the connection string to the ApplyURI() method:

client, err := mongo.Connect(options.Client().ApplyURI(uri))
if err != nil {
log.Fatal(err)
}

The following code creates a client and sets the cluster options with a maximum server selection timeout of 10 seconds and a local threshold of 20 milliseconds:

clientOptions := options.Client().
ApplyURI(uri).
SetServerSelectionTimeout(10 * time.Second).
SetLocalThreshold(20 * time.Millisecond)
client, err := mongo.Connect(clientOptions)
if err != nil {
log.Fatal(err)
}

To learn more about the methods and types in this guide, see the following API documentation:

Back

Network Compression

On this page