Overview
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 |
|---|---|
| Specifies the maximum amount of time the driver will wait for a server to be available before throwing an error. Default: 30 seconds |
| Specifies the maximum latency in milliseconds for selecting a server. Default: 15 milliseconds |
| Specifies the name of the replica set to connect to. |
| Specifies whether to connect directly to a single server, bypassing the replica set or sharded cluster. Default: |
| Specifies whether the driver is connecting to MongoDB by using a load balancer. If set to You can set this option to
Default: |
| 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. |
| Specifies whether the driver deprioritizes a server that returns an overload error, reducing the likelihood of retrying on the same overloaded server. Default: |
| Specifies the maximum number of times the driver retries an operation that fails with a server-side overload error. Default: |
The following table describes several methods you can chain to your ClientOptions struct to modify the driver's behavior:
Method | Description |
|---|---|
| Specifies the maximum amount of time the driver will wait for a server to be available before throwing an error. Default: 30 seconds |
| Specifies the maximum latency in milliseconds for selecting a server. Default: 15 milliseconds |
| Specifies the name of the replica set to connect to. |
| Specifies whether to connect directly to a single server, bypassing the replica set or sharded cluster. Default: |
| Specifies whether the driver is connecting to MongoDB by using a load balancer. If set to You can set this option to
Default: |
| 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 |
| Specifies whether the driver deprioritizes a server that returns an overload error, reducing the likelihood of retrying on the same overloaded server. Default: |
| Specifies the maximum number of times the driver retries an operation that fails with a server-side overload error. Default: |
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:
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) }
API Documentation
To learn more about the methods and types in this guide, see the following API documentation: