Overview
This section describes the MongoDB connection and authentication options available in Rust driver. You can configure your connection by using either the connection URI (also called a connection string) or by setting fields on a ClientOptions instance when you create a Client.
Using the Connection URI
If you pass a connection URI to the Client::with_uri_str method, you can include connection options in the string as <name>=<value> pairs. The following example shows a connection URI containing the connectTimeoutMS option with a value of 60000 and the tls option with a value of true:
use mongodb::Client; let uri = "mongodb://<hostname>:<port>/?connectTimeoutMS=60000&tls=true"; let client = Client::with_uri_str(uri).await?;
Using ClientOptions
You can set connection options on a ClientOptions instance instead of including them in your connection URI. When you configure your connection using a ClientOptions instance, it is easier for you to change settings at runtime and catch errors during compilation. The following example shows how to use ClientOptions to set connection options:
use std::time::Duration; use mongodb::{Client, options::ClientOptions}; let uri = "mongodb://<hostname>:<port>/"; let mut client_options = ClientOptions::parse(uri).await?; client_options.connect_timeout = Some(Duration::from_secs(60000)); //Set additional options on client_options here let client = Client::with_options(client_options)?;
Connection Options
The following sections describe the connection options available in the Rust driver. To see a full list of connection options, visit the Connection String Options section of the Server manual guide on Connection Strings.
Network Compression
Option Name | Accepted Values | Default Value | Description |
|---|---|---|---|
compressors | A comma-separated list of strings | None | Specifies compressors that the |
zlibCompressionLevel | Integer between |
| Specifies the level field of the |
Timeout
Option Name | Accepted Values | Default Value | Description |
|---|---|---|---|
connectTimeoutMS | Non-negative integer |
| Specifies the connection timeout, in milliseconds, passed to each underlying TCP stream when attempting to connect to the server. |
Server Selection
Option Name | Accepted Values | Default Value | Description |
|---|---|---|---|
heartbeatFrequencyMS | Integer greater than or equal to 500 |
| Specifies the amount of time in milliseconds that each monitoring thread waits between performing server checks. |
maxStalenessSeconds |
|
| Specifies the maximum lag, in seconds, behind the primary
node that a secondary node can be considered for the given
operation. |
serverSelectionTimeoutMS | Non-negative integer |
| Specifies the amount of time in milliseconds that the |
Authentication
Option Name | Accepted Values | Default Value | Description |
|---|---|---|---|
authMechanism | String | None | Specifies which authentication mechanism to use. If you do not specify this option, the driver uses the default authentication mechanism. To learn more about authentication in the Rust driver, see the guide on Authentication Mechanisms. |
authMechanismProperties | String | None | Specifies more properties for the authentication mechanism set in the |
authSource | String | See description | Specifies the database used to authenticate. |
tls | Boolean |
| Specifies the TLS configuration for the |
tlsAllowInvalidCertificates | Boolean |
| Specifies whether the |
tlsCAFile | String | See description | Specifies the path to the certificate authority (CA) file that
the |
tlsCertificateKeyFile | String | None | Specifies the path to the certificate file that the |
tlsCertificateKeyFilePassword | String | None | Specifies the password to decrypt the private key in your certificate file, if the key is encrypted. |
tlsInsecure | Boolean |
| Specifies whether the |
Read and Write Operations
Option Name | Accepted Values | Default Value | Description |
|---|---|---|---|
directConnection | Boolean |
| Specifies whether the |
enableOverloadRetargeting | Boolean |
| Specifies if the driver deprioritizes a server that returns an overload error, reducing the likelihood of retrying on the same overloaded server. |
journal | Boolean |
| Requests acknowledgment that the operation propagated to the on-disk journal. |
localThresholdMS | Non-negative integer |
| Specifies the amount of time in milliseconds that the average
round-trip time between the driver and server can last
compared to the shortest round-trip time of all the suitable
servers. |
maxAdaptiveRetries | Non-negative integer |
| Specifies the maximum number of retries to attempt when the driver encounters overload errors. |
readConcernLevel | String | None | Specifies the default read concern for operations performed
on the |
readPreference | String |
| Specifies how the driver routes a read operation to members
of a replica set. |
readPreferenceTags | A list of comma-separated key-value pairs | None | Specifies which replica set members are considered for
operations. Each instance of this key is a separate tag set. |
replicaSet | String | None | Specifies the name of the replica set that the |
retryReads | Boolean |
| Specifies whether the client retries a read operation if the operation fails. |
w | Non-negative integer or string | None | Requests acknowledgment that the operation has propagated to a
specific number or variety of servers. |
wTimeoutMS | Non-negative integer | No timeout | Specifies a time limit, in milliseconds, for the write
concern. |
Connection Pools
Option Name | Accepted Values | Default Value | Description |
|---|---|---|---|
maxIdleTimeMS | Non-negative integer |
| Specifies the amount of time in milliseconds that a
connection can remain idle in a connection pool before the
server closes it. |
maxPoolSize | Non-negative integer |
| Specifies the maximum number of connections that the
|
minPoolSize | Non-negative integer |
| Specifies the minimum number of connections available in a
server's connection pool at a given time. |
For more information on connection pools, see the connection pools guide.
General
Option Name | Accepted Values | Default Value | Description |
|---|---|---|---|
appName | String | None | Specifies the application name that the |
API Documentation
For more information about ClientOptions for the Rust driver, see the API documentation for ClientOptions.