Docs Menu
Docs Home
/ / /
Go Driver
/ /

Customize Cluster Settings

In this guide, you can learn how the Go driver manages clusters.

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

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

Method
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 or not 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 some of the methods you can chain to your settings to modify the driver's behavior:

Field
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 or not 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 15 milliseconds:

// Connection string with cluster settings options
const (
uri = "mongodb://localhost:27017/?serverSelectionTimeoutMS=10000&localThresholdMS=15"
)

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

// Creates a new client and connects to the server
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 15 milliseconds:

// Sets client options with cluster settings
clientOptions := options.Client().
ApplyURI(uri).
SetServerSelectionTimeout(10 * time.Second).
SetLocalThreshold(15 * time.Millisecond)
// Creates a new client and connects to the server
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