Overview
This section describes the MongoDB connection and authentication options available in Ruby driver. You can configure your connection by using either the connection URI (also called a connection string) or by passing arguments to the Mongo::Client constructor.
Using the Connection URI
If you pass a connection URI to the Mongo::Client constructor, you can include connection options in the string as <name>=<value> pairs. In the following example, the connection URI contains the connectTimeoutMS option with a value of 60000 and the tls option with a value of true:
uri = "mongodb://<hostname>:<port>/?connectTimeoutMS=60000&tls=true" client = Mongo::Client.new(uri)
Using a Mongo::Client
You can pass connection options as arguments to the Mongo::Client constructor instead of including them in your connection URI. When you configure your connection this way, it easier for you to change settings at runtime and catch errors during compilation. The following example shows how to use the Mongo::Client constructor to set connection options:
uri = "mongodb://<hostname>:<port>" client = Mongo::Client.new(uri, connect_timeout: 60000, ssl: true)
Connection Options
The following sections describe the connection options available in the Ruby driver.
Network Compression
Connection Option | Description |
|---|---|
:compressors | A list of potential compressors to use, in order of preference.
The driver chooses the first compressor that is also supported
by the server. Currently the driver only supports |
:zlib_compression_level | The Zlib compression level to use, if using compression.
This option accepts an integer value between |
Timeouts
Connection Option | Description |
|---|---|
:connect_timeout | The number of seconds to wait to establish a socket
connection before raising an exception. This
timeout is also used for SRV DNS record resolution. |
:timeout_ms | The number of milliseconds to wait for an operation to execute
before raising an exception. |
Server Selection
Connection Option | Description |
|---|---|
:load_balanced | Whether to expect to connect to a load balancer. |
:server_monitoring_mode | The server monitoring protocol to use. When
this option is set to Data Type: |
:server_selection_timeout | The maximum amount of time, in seconds, the driver waits
for server selection to succeed before throwing an exception. |
For more information on server selection, see the Server Selection guide.
Authentication
Connection Option | Description |
|---|---|
:auth_mech | The mechanism that the Ruby driver uses to authenticate the application. |
:auth_mech_properties | Options specific to the authentication mechanism. This option
isn't needed for all authentication mechanisms. |
:auth_source | The database to authenticate against. |
:user | The username for authentication. When this option is included
in a connection URI, you must
percent-encode it. |
:password | The password for authentication. When this option is included
in a connection URI, you must
percent-encode it. |
Read and Write Operations
Connection Option | Description |
|---|---|
:replica_set | Specifies the name of the replica set to connect to. |
:direct_connection | Whether to connect directly to the specified host |
:enable_overload_retargeting | Whether the driver deprioritizes a server that returns an
overload error, reducing the likelihood of retrying on the
same overloaded server. |
:max_adaptive_retries | The maximum number of retries to attempt when the driver
encounters overload errors. |
:read | The read preference options. For more information,
see Read Preference in the MongoDB Server manual. |
:read_concern | Specifies the read concern options. For more information, see
Read Concern in the MongoDB Server manual. |
:write_concern | Specifies the client's write concern. For more
information, see Write Concern in
the MongoDB Server manual. |
:local_threshold | The latency window, in seconds, for a replica-set member's
eligibility. If a member's round trip ping takes longer
than the fastest server's round-trip ping time
plus this value, the server isn't eligible for selection. |
Connection Pools
Connection Option | Description |
|---|---|
:max_pool_size | The maximum number of concurrent connections that the pool maintains.
If the number of in-use connections to a server reaches the specified
value, the next request to that server waits until a connection becomes
available. Setting this option to |
:max_connecting | The maximum number of connections that each pool can establish
concurrently. |
:min_pool_size | The minimum number of concurrent connections that the pool maintains. |
:max_idle_time | The maximum number of seconds that a connection can remain idle in
the pool. |
For more information on connection pools, see the Connection Pools guide.
API Documentation
For more information about Mongo::Client options for the Ruby driver, see the API documentation for Mongo::Client.