对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

指定连接选项

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.

如果将连接 URI 传递给 Mongo::Client 构造函数,则可以将连接选项作为 <name>=<value> 对包含在字符串中。在以下示例中,连接 URI 包含值为 60000connectTimeoutMS 选项和值为 truetls 选项:

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

您可以将连接选项作为参数传递给 Mongo::Client 构造函数,而不是将它们包含在连接 URI 中。以这种方式配置连接时,可以更轻松地在运行时更改设置并在编译期间捕获错误。以下示例演示如何使用 Mongo::Client 构造函数设立连接选项:

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

以下部分描述了Ruby驱动程序中可用的连接选项。

连接选项
说明

:compressors

要使用的潜在压缩器列表,按优先顺序排列。驱动程序会选择服务器也支持的第一个压缩器。目前,驱动程序仅支持 zstdsnappyzlib

数据类型Array<String>
默认:无
客户端示例compressors: ['snappy', 'zstd', 'zlib']
连接 URI 示例compressors=snappy,zstd,zlib

:zlib_compression_level

要使用的 Zlib 压缩级别(如果使用压缩)。此选项接受-19

之间的整数值:- -1 : zlib使用其默认压缩级别(通常是6 )。
-0 :无压缩。
-1 :速度最快,但压缩率最低。
-9 :压缩最佳,但速度最慢。有关更多信息,请参阅

Ruby 的 ZLib

模块文档。数据类型:Integer
None
默认值: 客户端示例:zlib_compression_level: 3
连接 URI 示例:zlibCompressionLevel=3

连接选项
说明

:connect_timeout

在提出异常之前等待建立套接字连接的秒数。此超时也用于 SRV DNS 记录解析。

nil0 表示无超时。如果传递了无效的超时值(例如负值或非数值),则客户端创建将失败并报错。

数据类型Float
默认10.0
客户端示例connect_timeout: 10.0
连接 URI 示例connectTimeoutMS=10000

:timeout_ms

在引发异常之前等待操作执行的毫秒数。

0 表示没有超时。如果传递了无效的超时值(例如负值或非数值),则客户端创建将失败并报错。

数据类型Integer
默认:无
客户端示例timeout_ms: 5000
连接 URI 示例timeoutMS=5000

连接选项
说明

:load_balanced

是否预计连接到负载均衡器。

数据类型Boolean
默认false
客户端示例load_balanced: { true }
连接 URI 示例:不适用

:server_monitoring_mode

要使用的服务器监控协议。当此选项设置为 :auto 时,监控模式由驱动程序运行的环境决定。该驱动程序在功能即服务 (FaaS) 环境(例如 Amazon Web Services Lambda)中使用轮询模式,在其他环境中使用流媒体模式。

数据类型Symbol
默认:auto
客户端示例server_monitoring_mode: :poll
连接 URI 示例serverMonitoringMode=poll

:server_selection_timeout

驱动程序在抛出异常之前等待服务器选择成功的最长时间(以秒为单位)。

数据类型Integer
默认30
客户端示例server_selection_timeout: 30
连接 URI 示例serverSelectionTimeoutMS=30000

有关服务器选择的更多信息,请参阅服务器选择指南

连接选项
说明

:auth_mech

Ruby 驱动程序用于验证应用程序的机制。

数据类型Symbol
默认:scram256
如果未提供用户凭证,则为 nil
客户端示例auth_mech: :scram256
连接 URI 示例authMechanism=SCRAM-SHA-256

:auth_mech_properties

针对身份验证机制的选项。此选项并非所有身份验证机制都需要。

数据类型Hash
默认:使用 GSSAPI 身份验证机制时,
默认属性为 {service_name: "mongodb"}
否则,默认为 nil
客户端示例auth_mech_properties: {aws_session_token: '12345'}
连接 URI 示例authMechanismProperties=AWS_SESSION_TOKEN:12345

:auth_source

要认证的数据库。

数据类型String
默认admin,如果提供了凭证
客户端示例auth_source: admin
连接 URI 示例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

指定要连接的副本集的名称。

数据类型String
默认值:无
客户端示例replica_set: 'myRS'
连接 URI 示例replicaSet=myRS

:direct_connection

是否直接连接到指定的主机

数据类型Boolean
默认false
客户端示例direct_connection: true
连接 URI 示例directConnection=true

:enable_overload_retargeting

驱动程序是否会降低返回超负载错误的服务器的优先级,从而降低在同一个超负载服务器上重试的可能性。

数据类型Boolean
默认false
客户端示例enable_overload_retargeting: true
连接 URI 示例enableOverloadRetargeting=true

:max_adaptive_retries

当驱动程序遇到超负载错误时尝试的最大重试次数。

数据类型Integer
默认2
客户端示例max_adaptive_retries: 3
连接 URI 示例maxAdaptiveRetries=3

: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

副本集节点资格的延迟窗口,以秒为单位。如果节点的往返网络探测(ping)时间超过最快服务器的往返网络探测(ping)时间加上此值,则该服务器没有资格被选中。

数据类型Float
默认0.015
客户端示例local_threshold: 0.020
连接 URI 示例localThresholdMS=20

连接选项
说明

:max_pool_size

连接池维护的并发连接最大数。如果某个服务器的在用连接数达到指定值,则对该服务器的下一个请求将等待,直到出现可用连接。将此选项设置为 0会创建一个无限连接池。

数据类型Integer
默认值100
客户端示例max_pool_size: 100
连接 URI 示例maxPoolSize=100

:max_connecting

每个池可以同时建立的最大连接数。

数据类型Integer
默认2
客户端示例max_connecting: 2
连接 URI 示例maxConnecting=2

:min_pool_size

连接池维护的并发连接最小数。

数据类型Integer
默认0
客户端示例min_pool_size: 1
连接 URI 示例minPoolSize=1

:max_idle_time

连接在连接池中可以保持空闲状态的最大秒数。

数据类型Integer
默认0 (无限制)
客户端示例max_idle_time: 10
连接 URI 示例maxIdleTimeMS=10000

有关连接池的更多信息,请参阅 连接池指南。

有关Ruby驱动程序的 Mongo::Client 选项的更多信息,请参阅 Mongo::客户端 的API文档。