In this guide, you can learn how the Go driver manages clusters and how
to customize the cluster settings.
Specify 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 by using a load
balancer. If set to true, the driver assumes that it is
connecting through a load balancer.
You can set this option to true only if you meet the following requirements:
You specify only one host name
You aren't connecting to a replica set
The srvMaxHosts option is unset or has a value of 0
The directConnection option is unset or has a value of false
Specifies whether the driver deprioritizes a server that
returns an overload error, reducing the likelihood of
retrying on the same overloaded server.
Default:false
maxAdaptiveRetries
Specifies the maximum number of times the driver retries
an operation that fails with a server-side overload error.
Default:2
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 by using a
load balancer. If set to true, the driver assumes that it is
connecting through a load balancer.
You can set this option to true only if you meet the following requirements:
You specify only one host name
You aren't connecting to a replica set
The srvMaxHosts option is unset or has a value of 0
The directConnection option is unset or has a value of false
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().
SetEnableOverloadRetargeting()
Specifies whether the driver deprioritizes a server that
returns an overload error, reducing the likelihood of
retrying on the same overloaded server.
Default:false
SetMaxAdaptiveRetries()
Specifies the maximum number of times the driver retries
an operation that fails with a server-side overload error.
Default:2
To learn more about the available methods, see the API Documentation section.
Example
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:
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)
}
API Documentation
To learn more about the methods and types in this guide, see the following API documentation: