For AI agents: a documentation index is available at https://www.mongodb.com/pt-br/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.

Specify Connection Options

This page describes the connection options available in the Go driver and explains how to apply them to your MongoDB connection.

The following sections describe how you can specify connection options by using a connection string or a ClientOptions struct.

You can specify connection options in a connection string by passing a ClientOptions struct to the Connect() method. In the connection string, you can include connection options in the string as <name>=<value> pairs. In the following example, the connection string contains the connectTimeoutMS option with a value of 60000 milliseconds and the tls option with a value of true:

const uri = "mongodb+srv://localhost:27017/?connectTimeoutMS=60000&tls=true"client, _ := mongo.Connect(options.Client().ApplyURI(uri))

You can also chain connection options to the ClientOptions struct to configure connection settings. Configuring the connection this way makes it easier to change settings at runtime, helps you catch errors during compilation, and provides more configuration options than the connection string.

The following code example sets the Timeout option to 60 seconds and enables TLS by passing an empty tls.Config struct to the SetTLSConfig() method:

opts := options.Client(). SetConnectTimeout(60 * time.Second). SetTLSConfig(&tls.Config{}) client, _ := mongo.Connect(opts)

Data Type: bool

Default Value: false

Example:

const uri = "mongodb://localhost:27017/?directConnection=true"client, _ := mongo.Connect(options.Client().ApplyURI(uri))

Data Type: string

Default Value: nil

Example:

const uri = "mongodb://localhost:27017/?replicaSet=yourReplicaSet"client, _ := mongo.Connect(options.Client().ApplyURI(uri))

Data Type: bool

Default Value: nil

Example:

const uri = "mongodb://localhost:27017/?tls=true"client, _ := mongo.Connect(options.Client().ApplyURI(uri))

The tlsDisableCertificateRevocationCheck option disables certificate revocation checking for TLS connections. When you set this option to true, the driver does not verify the revocation status of certificates but still validates the certificate chain and hostname. Setting this option implicitly enables TLS, even when you set it to false. Do not combine this option with tlsInsecure or tlsDisableOCSPEndpointCheck.

Data Type: bool

Default Value: false

Example:

uri := os.Getenv("MONGODB_URI")if uri == "" { log.Fatal("Set your 'MONGODB_URI' environment variable")} // Use an ampersand instead of a question mark if the connection string// already contains query options.separator := "?"if strings.Contains(uri, "?") { separator = "&"} uriOptions := options.Client(). ApplyURI(uri + separator + "tlsDisableCertificateRevocationCheck=true")

For more information on TLS options, see the Enable TLS on a Connection guide.

Data Type: non-negative int

Default Value: 30000 milliseconds

Example:

const uri = "mongodb://localhost:27017/?connectTimeoutMS=60000"client, _ := mongo.Connect(options.Client().ApplyURI(uri))

The timeoutMS option specifies the amount of time that a single operation run on the client can execute before returning an error.

Data Type: non-negative int

Default Value: nil

Example:

const uri = "mongodb://localhost:27017/?timeoutMS=30000"client, _ := mongo.Connect(options.Client().ApplyURI(uri))

Data Type: string (values separated by commas)

Default Value: nil

Example:

const uri = "mongodb://localhost:27017/?compressors=zlib,snappy"client, _ := mongo.Connect(options.Client().ApplyURI(uri))

The zlibCompressionLevel option sets the compression level for zlib to use. This option is ignored if zlib is not specified as a compressor through ApplyURI or SetCompressors. This option accepts an integer value between -1 and 9:

  • -1: (Default). zlib uses its default compression level (usually 6).

  • 0: No compression.

  • 1: Fastest speed but lowest compression.

  • 9: Best compression but slowest speed.

Data Type: int

Default Value: -1

Example:

const uri = "mongodb://localhost:27017/?compressors=zlib&zlibCompressionLevel=6"client, _ := mongo.Connect(options.Client().ApplyURI(uri))

The zstdCompressionLevel option sets the compression level for zstd to use. This option is ignored if zstd is not specified as a compressor through ApplyURI or SetCompressors. This option accepts integer values between 1 and 20:

  • 1: Fastest speed but lowest compression.

  • 20: Best compression but slowest speed.

Data Type: int

Default Value: 6

Example:

const uri = "mongodb://localhost:27017/?compressors=zstd&zstdCompressionLevel=6"client, _ := mongo.Connect(options.Client().ApplyURI(uri))

For more information on compression, see the Compress Network Traffic guide.

Data Type: non-negative int

Default Value: 100

Example:

const uri = "mongodb://localhost:27017/?maxPoolSize=150"client, _ := mongo.Connect(options.Client().ApplyURI(uri))

Data Type: non-negative int

Default Value: 0

Example:

const uri = "mongodb://localhost:27017/?minPoolSize=3"client, _ := mongo.Connect(options.Client().ApplyURI(uri))

Data Type: non-negative int

Default Value: 0

Example:

const uri = "mongodb://localhost:27017/?maxIdleTimeMS=8000"client, _ := mongo.Connect(options.Client().ApplyURI(uri))

The maxConnecting option sets the maximum number of connections a connection pool may establish simultaneously.

Data Type: non-negative int

Default Value: 2

Example:

const uri = "mongodb://localhost:27017/?maxConnecting=3"client, _ := mongo.Connect(options.Client().ApplyURI(uri))

To learn more about connection pools, see the Connection Pools guide.

Data Type: int or string

Default Value: 1 or "majority"

Example:

const uri = "mongodb://localhost:27017/?w=2"client, _ := mongo.Connect(options.Client().ApplyURI(uri))

Data Type: string

Default Value: local

Example:

const uri = "mongodb://localhost:27017/?readConcernLevel=majority"client, _ := mongo.Connect(options.Client().ApplyURI(uri))

Data Type: string

Default Value: primary

Example:

const uri = "mongodb://localhost:27017/?readPreference=primaryPreferred"client, _ := mongo.Connect(options.Client().ApplyURI(uri))

To learn more about available authentication mechanisms, see the Authentication Mechanism guides.

Data Type: string

Default Value: empty (no authentication), or SCRAM-SHA-256 once authentication is enabled

Example:

const uri = "mongodb://user:password@localhost:27017/?authMechanism=PLAIN&authSource=admin"client, _ := mongo.Connect(options.Client().ApplyURI(uri))

Data Type: non-negative int

Default Value: 30000 milliseconds

Example:

const uri = "mongodb://localhost:27017/?serverSelectionTimeoutMS=40000"client, _ := mongo.Connect(options.Client().ApplyURI(uri))

Data Type: non-negative int

Default Value: 15 milliseconds

Example:

const uri = "mongodb://localhost:27017/?localThresholdMS=20000"client, _ := mongo.Connect(options.Client().ApplyURI(uri))

Data Type: bool

Default Value: true

Example:

const uri = "mongodb://localhost:27017/?retryReads=false"client, _ := mongo.Connect(options.Client().ApplyURI(uri))

Data Type: bool

Default Value: true

Example:

const uri = "mongodb://localhost:27017/?retryWrites=false"client, _ := mongo.Connect(options.Client().ApplyURI(uri))

The maxAdaptiveRetries option sets the maximum number of times the driver should retry operations that fail with a server-side overload error.

Data Type: non-negative int

Default Value: 2

Example:

const uri = "mongodb://localhost:27017/?maxAdaptiveRetries=3"client, _ := mongo.Connect(options.Client().ApplyURI(uri))

Data Type: string

Default Value: nil

Example:

const uri = "mongodb://localhost:27017/?appName=yourAppName"client, _ := mongo.Connect(options.Client().ApplyURI(uri))

Data Type: bool

Default Value: false

Example:

const uri = "mongodb://localhost:27017/?loadBalanced=true"client, _ := mongo.Connect(options.Client().ApplyURI(uri))

Data Type: non-negative int

Default Value: 0

Example:

opts := options.Client(). SetSRVMaxHosts(5) client, _ := mongo.Connect(opts)

Data Type: string

Default Value: mongodb

Example:

const uri = "mongodb+srv://localhost/?srvServiceName=yourServiceName"client, _ := mongo.Connect(options.Client().ApplyURI(uri))

To learn more about the options that you can specify in a connection string, see Connection String Options in the MongoDB Server manual.

For more information about the types used on this page, see the following API documentation: