Overview
This page describes the connection options available in the Go driver and explains how to apply them to your MongoDB connection.
How to Specify Connection Options
The following sections describe how you can specify connection options by using a connection string or a ClientOptions struct.
Use the Connection String
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))Use the ClientOptions Struct
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)Replica Set Options
Direct Connection
Data Type: bool
Default Value: false
Example:
const uri = "mongodb://localhost:27017/?directConnection=true"client, _ := mongo.Connect(options.Client().ApplyURI(uri))Replica Set
Data Type: string
Default Value: nil
Example:
const uri = "mongodb://localhost:27017/?replicaSet=yourReplicaSet"client, _ := mongo.Connect(options.Client().ApplyURI(uri))TLS Options
Enable TLS
Data Type: bool
Default Value: nil
Example:
const uri = "mongodb://localhost:27017/?tls=true"client, _ := mongo.Connect(options.Client().ApplyURI(uri))Disable Certificate Revocation
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")Enable TLS
Data Type: tls.Config
Default Value: false
Example:
opts := options.Client(). SetTLSConfig(&tls.Config{})
client, _ := mongo.Connect(opts)Disable Certificate Revocation
The SetDisableCertificateRevocationCheck() method 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 does not implicitly enable TLS. Do not combine this option with tlsInsecure or tlsDisableOCSPEndpointCheck.
Data Type: bool
Default Value: false
Example:
settingsOptions := options.Client(). SetDisableCertificateRevocationCheck(true)For more information on TLS options, see the Enable TLS on a Connection guide.
Timeout Options
Connect Timeout
Data Type: non-negative int
Default Value: 30000 milliseconds
Example:
const uri = "mongodb://localhost:27017/?connectTimeoutMS=60000"client, _ := mongo.Connect(options.Client().ApplyURI(uri))Timeout
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))Connect Timeout
Data Type: time.Duration
Default Value: 30 seconds
Example:
opts := options.Client(). SetConnectTimeout(60 * time.Second)
client, _ := mongo.Connect(opts)Timeout
The SetTimeout() method specifies the amount of time that a single operation run on the client can execute before returning an error.
Data Type: time.Duration
Default Value: nil
Example:
opts := options.Client(). SetTimeout(30 * time.Second)
client, _ := mongo.Connect(opts)Compression Options
Compressors
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))Zlib Compression Level
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).zlibuses its default compression level (usually6).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))Zstandard Compression Level
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))Compressors
Data Type: []string
Default Value: nil
Example:
opts := options.Client(). SetCompressors([]string{"zlib", "snappy"})
client, _ := mongo.Connect(opts)Zlib Compression Level
The SetZlibLevel() method 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).
zlibuses its default compression level (usually6).0: No compression.
1: Fastest speed but lowest compression.
9: Best compression but slowest speed.
Data Type: int
Default Value: -1
Example:
opts := options.Client(). SetCompressors([]string{"zlib"}). SetZlibLevel(6)
client, _ := mongo.Connect(opts)Zstandard Compression Level
The SetZstdLevel() method 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:
Data Type: int
Default Value: 6
Example:
opts := options.Client(). SetCompressors([]string{"zstd"}). SetZstdLevel(8)
client, _ := mongo.Connect(opts)For more information on compression, see the Compress Network Traffic guide.
Connection Pool Options
Max Pool Size
Data Type: non-negative int
Default Value: 100
Example:
const uri = "mongodb://localhost:27017/?maxPoolSize=150"client, _ := mongo.Connect(options.Client().ApplyURI(uri))Min Pool Size
Data Type: non-negative int
Default Value: 0
Example:
const uri = "mongodb://localhost:27017/?minPoolSize=3"client, _ := mongo.Connect(options.Client().ApplyURI(uri))Max Connection Idle Time
Data Type: non-negative int
Default Value: 0
Example:
const uri = "mongodb://localhost:27017/?maxIdleTimeMS=8000"client, _ := mongo.Connect(options.Client().ApplyURI(uri))Max Connecting
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))Max Pool Size
Data Type: non-negative uint64
Default Value: 100
Example:
opts := options.Client(). SetMaxPoolSize(150)
client, _ := mongo.Connect(opts)Min Pool Size
Data Type: non-negative uint64
Default Value: 0
Example:
opts := options.Client(). SetMinPoolSize(3)
client, _ := mongo.Connect(opts)Max Connection Idle Time
Data Type: time.Duration
Default Value: 0
Example:
opts := options.Client(). SetMaxConnIdleTime(8 * time.Second)
client, _ := mongo.Connect(opts)Max Connecting
The SetMaxConnecting() method sets the maximum number of connections a connection pool may establish simultaneously.
Data Type: non-negative uint64
Default Value: 2
Example:
opts := options.Client(). SetMaxConnecting(3)
client, _ := mongo.Connect(opts)To learn more about connection pools, see the Connection Pools guide.
Write Concern Options
Read Concern Options
Read Preference Options
Authentication Options
Authentication Mechanism
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))Authentication Mechanism
To learn more about available authentication mechanisms, see the Authentication Mechanism guides.
Data Type: Credential
Default Value: empty (no authentication), or SCRAM-SHA-256 once authentication is enabled
Example:
credential := options.Credential{ AuthMechanism: "PLAIN", AuthSource: "admin", Username: "user", Password: "password",}opts := options.Client().SetAuth(credential)
client, _ := mongo.Connect(opts)Server Selection Options
Server Selection Timeout
Data Type: non-negative int
Default Value: 30000 milliseconds
Example:
const uri = "mongodb://localhost:27017/?serverSelectionTimeoutMS=40000"client, _ := mongo.Connect(options.Client().ApplyURI(uri))Local Threshold
Data Type: non-negative int
Default Value: 15 milliseconds
Example:
const uri = "mongodb://localhost:27017/?localThresholdMS=20000"client, _ := mongo.Connect(options.Client().ApplyURI(uri))Server Selection Timeout
Data Type: time.Duration
Default Value: 30 seconds
Example:
opts := options.Client(). SetServerSelectionTimeout(40 * time.Second)
client, _ := mongo.Connect(opts)Local Threshold
Data Type: time.Duration
Default Value: 15 milliseconds
Example:
opts := options.Client(). SetLocalThreshold(20 * time.Millisecond)
client, _ := mongo.Connect(opts)Retry Options
Retry Reads
Data Type: bool
Default Value: true
Example:
const uri = "mongodb://localhost:27017/?retryReads=false"client, _ := mongo.Connect(options.Client().ApplyURI(uri))Retry Writes
Data Type: bool
Default Value: true
Example:
const uri = "mongodb://localhost:27017/?retryWrites=false"client, _ := mongo.Connect(options.Client().ApplyURI(uri))Max Adaptive Retries
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))Retry Reads
Data Type: bool
Default Value: true
Example:
opts := options.Client(). SetRetryReads(false)
client, _ := mongo.Connect(opts)Retry Writes
Data Type: bool
Default Value: true
Example:
opts := options.Client(). SetRetryWrites(false)
client, _ := mongo.Connect(opts)Max Adaptive Retries
The SetMaxAdaptiveRetries() method sets the maximum number of times the driver should retry operations that fail with a server-side overload error.
Data Type: non-negative uint
Default Value: 2
Example:
opts := options.Client(). SetMaxAdaptiveRetries(3)
client, _ := mongo.Connect(opts)Miscellaneous Options
Application Name
Data Type: string
Default Value: nil
Example:
const uri = "mongodb://localhost:27017/?appName=yourAppName"client, _ := mongo.Connect(options.Client().ApplyURI(uri))Load Balanced
Data Type: bool
Default Value: false
Example:
const uri = "mongodb://localhost:27017/?loadBalanced=true"client, _ := mongo.Connect(options.Client().ApplyURI(uri))SRV Max Hosts
Data Type: non-negative int
Default Value: 0
Example:
opts := options.Client(). SetSRVMaxHosts(5)
client, _ := mongo.Connect(opts)SRV Service Name
Data Type: string
Default Value: mongodb
Example:
const uri = "mongodb+srv://localhost/?srvServiceName=yourServiceName"client, _ := mongo.Connect(options.Client().ApplyURI(uri))Application Name
Data Type: string
Default Value: nil
Example:
opts := options.Client(). SetAppName("yourAppName")
client, _ := mongo.Connect(opts)Load Balanced
Data Type: bool
Default Value: false
Example:
opts := options.Client(). SetLoadBalanced(true)
client, _ := mongo.Connect(opts)Server API
The SetServerAPIOptions() method configures the API version sent to the server when running commands. For more information on the Server API, see the Stable API guide.
Data Type: ServerAPIOptions
Default Value: nil
Example:
opts := options.Client(). SetServerAPIOptions(options.ServerAPI(options.ServerAPIVersion1))
client, _ := mongo.Connect(opts)SRV Max Hosts
Data Type: non-negative int
Default Value: 0
Example:
opts := options.Client(). SetSRVMaxHosts(5)
client, _ := mongo.Connect(opts)SRV Service Name
Data Type: string
Default Value: mongodb
Example:
opts := options.Client(). SetSRVServiceName("yourServiceName")
client, _ := mongo.Connect(opts)Additional Information
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: