-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP-49621 Customize cluster settings #522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
stephmarie17
merged 9 commits into
mongodb:comp-cov
from
stephmarie17:docsp-49621-serverSelection
Jul 2, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6b4b1f8
create guide and code examples
stephmarie17 f241f8c
Merge branch 'comp-cov' into docsp-49621-serverSelection
stephmarie17 a8504af
update toc and overview
stephmarie17 b163e77
vale fixes
stephmarie17 2ecbf24
edits
stephmarie17 a8c3163
nr feedback
stephmarie17 9f5ec47
fix url typo
stephmarie17 f86117e
nr feedback
stephmarie17 be26ae9
tech feedback
stephmarie17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
.. _golang-cluster-settings: | ||
|
||
========================== | ||
Customize Cluster Settings | ||
========================== | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: code example, connection string, connection options | ||
|
||
Overview | ||
-------- | ||
|
||
In this guide, you can learn how the {+driver-short+} 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 :guilabel:`Connection String` or :guilabel:`ClientOptions` tab to see the | ||
available options. | ||
|
||
.. tabs:: | ||
|
||
.. tab:: Connection String | ||
:tabid: uri | ||
|
||
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: | ||
|
||
.. list-table:: | ||
:header-rows: 1 | ||
:stub-columns: 1 | ||
:widths: 40 60 | ||
|
||
* - 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 | ||
<https://www.rfc-editor.org/rfc/rfc2782>`__ the driver retrieves to | ||
construct your :manual:`seed list | ||
</reference/glossary/#std-term-seed-list>`. You must use the | ||
:manual:`DNS Seed List Connection Format | ||
</reference/connection-string/#srv-connection-format>` in | ||
your connection string to use this option. | ||
|
||
.. tab:: ClientOptions | ||
:tabid: options | ||
|
||
The following table describes several methods you can chain to your | ||
``ClientOptions`` struct to modify the driver's behavior: | ||
|
||
.. list-table:: | ||
:header-rows: 1 | ||
:stub-columns: 1 | ||
:widths: 40 60 | ||
|
||
* - 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 | ||
<https://www.rfc-editor.org/rfc/rfc2782>`__ the driver retrieves to | ||
construct your :manual:`seed list | ||
</reference/glossary/#std-term-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 :ref:`golang-cluster-settings-resources` section. | ||
|
||
Example | ||
~~~~~~~ | ||
|
||
Select the :guilabel:`Connection String` or :guilabel:`ClientOptions` tab to | ||
see the corresponding example: | ||
|
||
.. tabs:: | ||
|
||
.. tab:: Connection String | ||
:tabid: uriExample | ||
|
||
The following code uses the connection string to configure the maximum | ||
server selection timeout to 10 seconds and the local threshold to 20 | ||
milliseconds: | ||
|
||
.. literalinclude:: /includes/connect/cluster-settings-uri.go | ||
:language: go | ||
:start-after: start-uri-variable | ||
:end-before: end-uri-variable | ||
:dedent: | ||
|
||
The following code creates a client and passes the connection string to the | ||
stephmarie17 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``ApplyURI()`` method: | ||
|
||
.. literalinclude:: /includes/connect/cluster-settings-uri.go | ||
:language: go | ||
:start-after: start-apply-uri | ||
:end-before: end-apply-uri | ||
:dedent: | ||
|
||
.. tab:: ClientOptions | ||
:tabid: optionsExample | ||
|
||
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: | ||
|
||
.. literalinclude:: /includes/connect/cluster-settings-client-options.go | ||
:language: go | ||
:start-after: start-client-options | ||
:end-before: end-client-options | ||
:dedent: | ||
|
||
.. _golang-cluster-settings-resources: | ||
|
||
API Documentation | ||
----------------- | ||
|
||
To learn more about the methods and types in this guide, see the following API documentation: | ||
|
||
- `Client <{+api+}/mongo#Client>`__ | ||
- `ClientOptions <{+api+}/mongo/options#ClientOptions>`__ | ||
- `ApplyURI() <{+api+}/mongo/options#ClientOptions.ApplyURI>`__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
source/includes/connect/cluster-settings-client-options.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
"time" | ||
|
||
"go.mongodb.org/mongo-driver/v2/mongo" | ||
"go.mongodb.org/mongo-driver/v2/mongo/options" | ||
) | ||
|
||
func main() { | ||
uri := os.Getenv("MONGODB_URI") | ||
if uri == "" { | ||
log.Fatal("Set your 'MONGODB_URI' environment variable.") | ||
} | ||
|
||
// Sets client options with cluster settings | ||
// start-client-options | ||
clientOptions := options.Client(). | ||
ApplyURI(uri). | ||
SetServerSelectionTimeout(10 * time.Second). | ||
SetLocalThreshold(20 * time.Millisecond) | ||
|
||
client, err := mongo.Connect(clientOptions) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
// end-client-options | ||
|
||
defer func() { | ||
if err = client.Disconnect(context.TODO()); err != nil { | ||
log.Fatal(err) | ||
} | ||
}() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"go.mongodb.org/mongo-driver/v2/mongo" | ||
"go.mongodb.org/mongo-driver/v2/mongo/options" | ||
) | ||
|
||
// Connection string with cluster settings options | ||
stephmarie17 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// start-uri-variable | ||
const uri = "mongodb://localhost:27017/?serverSelectionTimeoutMS=10000&localThresholdMS=20" | ||
|
||
// end-uri-variable | ||
|
||
func main() { | ||
// Creates a new client and connects to the server | ||
// start-apply-uri | ||
client, err := mongo.Connect(options.Client().ApplyURI(uri)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
// end-apply-uri | ||
|
||
fmt.Println("Connected to MongoDB with cluster settings options") | ||
defer func() { | ||
if err = client.Disconnect(context.TODO()); err != nil { | ||
log.Fatal(err) | ||
} | ||
}() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.