cluster-settings

For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

<!-- Tab options on this page. Append to the .md URL to filter: ?tabs=<id,...> select specific tabs (e.g. ?tabs=nodejs,shell) ?allTabs=true include every tab (no param) default: one tab per tabset

Available tabs: other tabs: uri, options, uriExample, optionsExample -->

Customize Cluster Settings

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.

Connection String

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 Default: false
srvServiceName Specifies the service name of the SRV resource records the driver retrieves to construct your [seed list](https://www.mongodb.com/docs/manual/reference/glossary/#std-term-seed-list. You must use the [DNS Seed List Connection Format](https://www.mongodb.com/docs/manual/reference/connection-string/#srv-connection-format in your connection string to use this option.
enableOverloadRetargeting 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

Example

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

Connection String

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)
}

API Documentation

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

  • [Client](https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo#Client

  • [ClientOptions](https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo/options#ClientOptions

  • [ApplyURI()](https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo/options#ClientOptions.ApplyURI

  • [SetEnableOverloadRetargeting()](https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo/options#ClientOptions.SetEnableOverloadRetargeting

  • [SetMaxAdaptiveRetries()](https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo/options#ClientOptions.SetMaxAdaptiveRetries