Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ / /
Ruby ドライバー
/

接続オプションの指定

このセクションでは、 Rubyドライバーで使用できるMongoDB の接続オプションと認証オプションについて説明します。接続を構成するには、接続 URI( 接続文字列とも呼ばれます)を使用するか、Mongo::Client コンストラクターに引数を渡します。

Mongo::Client コンストラクターに接続 URI を渡す場合は、接続オプションを <name>=<value> ペアとして文字列に含めることができます。次の例では、接続 URI に、値が 60000connectTimeoutMS オプションと、値が truetls オプションが含まれています。

uri = "mongodb://<hostname>:<port>/?connectTimeoutMS=60000&tls=true"
client = Mongo::Client.new(uri)

接続 URI に接続オプションを含める代わりに、接続オプションを引数として Mongo::Client コンストラクターに渡すことができます。このように接続を構成すると、実行時に設定を変更し、コンパイル中にエラーを検出しやすくなります。次の例は、Mongo::Client コンストラクターを使用して接続オプションを設定する方法を示しています。

uri = "mongodb://<hostname>:<port>"
client = Mongo::Client.new(uri, connect_timeout: 60000, ssl: true)

次のセクションでは、 Rubyドライバーで使用できる接続オプションについて説明します。

接続オプション
説明

: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 zstd, snappy, and zlib.

Data Type: Array<String>
Default: none
Client Example: compressors: ['snappy', 'zstd', 'zlib']
Connection URI Example: compressors=snappy,zstd,zlib

:zlib_compression_level

The Zlib compression level to use, if using compression. This option accepts an integer value between -1 and 9:

- -1: 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: Integer
Default: None
Client Example: zlib_compression_level: 3
Connection URI Example: zlibCompressionLevel=3
接続オプション
説明

: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.

nil and 0 mean no timeout. Client creation will fail with an error if an invalid timeout value is passed (such as a negative value or a non-numeric value).

Data Type: Float
Default: 10.0
Client Example: connect_timeout: 10.0
Connection URI Example: connectTimeoutMS=10000

:timeout_ms

The number of milliseconds to wait for an operation to execute before raising an exception.

0 means no timeout. Client creation will fail with an error if an invalid timeout value is passed (such as a negative value or a non-numeric value).

Data Type: Integer
Default: none
Client Example: timeout_ms: 5000
Connection URI Example: timeoutMS=5000
接続オプション
説明

:server_selector

Get the server selector. It either uses the read preference defined in the client options or defaults to a Primary server selector.

For more information on read preference modes, see the Server Selection Algorithm documentation in the MongoDB Server manual.

Data Type: ServerSelector
Default: none
Client Example: server_selector: { mode: :secondary_preferred }
Connection URI Example: N/A

:server_Selection_timeout

The maximum amount of time, in seconds, the driver waits for server selection to succeed before throwing an exception.

Data Type: Integer
Default: 30
Client Example: server_selection_timeout: 30
Connection URI Example: serverSelectionTimeoutMS=30000
接続オプション
説明

:auth_mech

The mechanism that the Ruby driver uses to authenticate the application.

Data Type: Symbol
Default: :scram256
nil if user credentials are not supplied.
Client Example: auth_mech: :scram256
Connection URI Example: authMechanism=SCRAM-SHA-256

:auth_mech_properties

Options specific to the authentication mechanism. This option isn't needed for all authentication mechanisms.

Data Type: Hash
Default: When you use the GSSAPI authentication mechanism,
the default properties are {service_name: "mongodb"}.
Otherwise, the default is nil.
Client Example: auth_mech_properties: {aws_session_token: '12345'}
Connection URI Example: authMechanismProperties=AWS_SESSION_TOKEN:12345

:auth_source

The database to authenticate against.

Data Type: String
Default: admin, if credentials are supplied
Client Example: auth_source: admin
Connection URI Example: authSource=admin

:user

The username for authentication. When this option is included in a connection URI, you must percent-encode it.

Data Type: String
Default: none
Client Example: user: my+user
Connection URI Example: username=my+user

:password

The password for authentication. When this option is included in a connection URI, you must percent-encode it.

Data Type: String
Default: none
Client Example: password: strong+password
Connection URI Example: password=strong+password
接続オプション
説明

:replica_set

Specifies the name of the replica set to connect to.

Data Type: String
Default: none
Client Example: replica_set: 'myRS'
Connection URI Example: replicaSet=myRS

:direct_ connection

Whether to connect directly to the specified host

Data Type: Boolean
Default: false
Client Example: direct_connection: true
Connection URI Example: directConnection=true

:read

The read preference options. For more information, see Read Preference in the MongoDB Server manual.

Data Type: Hash
Default: { mode: :primary }
Client Example: read: { mode: :primary }
Connection URI Example: readPreference=primary

:read_concern

Specifies the read concern options. For more information, see Read Concern in the MongoDB Server manual.

Data Type: Hash
Default: none
Client Example: read: { level: :majority }
Connection URI Example: readConcern=majority

:write_concern

Specifies the client's write concern. For more information, see Write Concern in the MongoDB Server manual.

Data Type: Hash
Default: write_concern: { w: 1 }
Client Example: write_concern: { w: 2 }
Connection URI Example: w=2

: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.

Data Type: Float
Default: 0.015
Client Example: local_threshold: 0.020
Connection URI Example: localThresholdMS=20

Rubyドライバーの Mongo::Client オプションの詳細については、Mongo::Client のAPIドキュメントを参照してください。

戻る

接続ターゲットの選択

項目一覧