Docs HomeNode.js

Connection Options连接选项

This section explains the MongoDB connection and authentication options supported by the driver. 本节介绍驱动程序支持的MongoDB连接和身份验证选项。You can pass the connection options as parameters of the connection URI to specify the behavior of the client.您可以将连接选项作为连接URI的参数传递,以指定客户端的行为。

Name名称Accepted Values接受的值Default Value默认值Description描述
appnamestringnullSpecifies the app name the driver passes to the server in the client metadata as part of the connection handshake. 指定驱动程序在客户端元数据中传递给服务器的应用程序名称,作为连接握手的一部分。The server prints the appname to the MongoDB logs upon establishing the connection. 服务器在建立连接时将appname打印到MongoDB日志中。It is also recorded in the slow query logs and profile collections.它也记录在慢速查询日志和配置文件集合中。
authMechanismstringnullSpecifies the authentication mechanism method to use for connection to the server. 指定用于连接到服务器的身份验证机制方法。If you do not specify a value, the driver uses the default mechanism, either SCRAM-SHA-1 or SCRAM-SHA-256 depending on the server version. 如果未指定值,驱动程序将使用默认机制,即SCRAM-SHA-1SCRAM-SHA-256,具体取决于服务器版本。See authentication mechanism for available authentication mechanisms.有关可用的身份验证机制,请参阅身份验证机制
authMechanismPropertiescomma separated key:value pairs, for example, "opt1:val1,opt2:val2"逗号分隔的键:值对,例如“opt1:val1,opt2:val2”nullSpecifies additional options provided for authentication, such as enabling hostname canonicalization for GSSAPI.指定为身份验证提供的其他选项,例如为GSSAPI启用主机名规范化。
authSourcestringnullSpecifies the database that connections should authenticate against.指定连接应针对的数据库进行身份验证。
compressorscomma separated list of strings, for example, "snappy,zlib,zstd"逗号分隔的字符串列表,例如“snappy,zlib,zstd”nullSpecifies the allowed compression types for wire protocol messages sent to or received from the server. 为发送到服务器或从服务器接收的有线协议消息指定允许的压缩类型。See Network Compression for more information.有关详细信息,请参阅网络压缩
connectTimeoutMSnon-negative integer非负整数30000Specifies the amount of time, in milliseconds, to wait to establish a single TCP socket connection to the server before raising an error. 指定在引发错误之前,与服务器建立单个TCP套接字连接所需的等待时间(以毫秒为单位)。Specifying 0 disables the connection timeout.指定0将禁用连接超时。
directConnectionbooleanfalseSpecifies whether to force dispatch all operations to the host specified in the connection URI.指定是否强制将所有操作分派到连接URI中指定的主机。
heartbeatFrequencyMSinteger greater than or equal to 500大于或等于500的整数nullSpecifies the interval, in milliseconds, between regular server monitoring checks.指定定期服务器监视检查之间的间隔(以毫秒为单位)。
journalbooleannullSpecifies the default write concern "j" field for the client. 指定客户端的默认写入关注"j"字段。See write concern for more information.有关更多信息,请参阅写入关注
loadBalancedbooleannullSpecifies whether the driver is connecting to a load balancer.指定驱动程序是否正在连接到负载平衡器。
localThresholdMSnon-negative integer非负整数15Specifies the size of the latency window, in milliseconds, on round trip time for selecting between suitable servers. 指定用于在合适的服务器之间进行选择的往返时间的延迟窗口大小(以毫秒为单位)。Specifying 0 means no wait, meaning the fastest available server.指定0表示无需等待,表示可用服务器速度最快。
maxIdleTimeMSnon-negative integer非负整数0Specifies the amount of time, in milliseconds, a connection can be idle before it's closed. 指定连接在关闭之前可以处于空闲状态的时间(以毫秒为单位)。Specifying 0 means no minimum.指定0表示没有最小值。
maxPoolSizenon-negative integer非负整数100Specifies the maximum number of clients or connections the driver can create in its connection pool. 指定驱动程序可以在其连接池中创建的客户端或连接的最大数量。This count includes connections in use.此计数包括正在使用的连接。
maxConnectingnon-negative integer非负整数2Specifies the maximum number of connections a driver's connection pool may be establishing concurrently.指定驱动程序的连接池可以同时建立的最大连接数。
maxStalenessSeconds-1, or an integer greater than or equal 90-1或大于等于90的整数nullSpecifies the maximum replication lag, in wall clock time, that a secondary can experience and still be eligible for server selection. 指定辅助服务器可以经历并且仍有资格选择服务器的最大复制延迟(以墙钟时间为单位)。Specifying -1 means no maximum.指定-1表示没有最大值。
minPoolSizenon-negative integer非负整数0Specifies the number of connections the driver should create and maintain in the connection pool even when no operations are occurring. 指定驱动程序应在连接池中创建和维护的连接数,即使没有任何操作。This count includes connections in use.此计数包括正在使用的连接。
proxyHoststringnullSpecifies the IPv4/IPv6 address or domain name of a SOCKS5 proxy server used for connecting to MongoDB services.指定用于连接MongoDB服务的SOCKS5代理服务器的IPv4/IPv6地址或域名。
proxyPortnon-negative integer非负整数nullSpecifies the port of the SOCKS5 proxy server specified in proxyHost.指定在proxyHost中指定的SOCKS5代理服务器的端口。
proxyUsernamestringnullSpecifies the username for username/password authentication to the SOCKS5 proxy server specified in proxyHost.指定对proxyHost中指定的SOCKS5代理服务器进行用户名/密码身份验证的用户名。
proxyPasswordstringnullSpecifies the password for username/password authentication to the SOCKS5 proxy server specified in proxyHost.指定对proxyHost中指定的SOCKS5代理服务器进行用户名/密码身份验证的密码。
readConcernLevelstringnullSpecifies the default read concern for the client. 指定客户端的默认读取问题。See read concern for more information.有关更多信息,请参阅读取关注
readPreferencestring"primary"Specifies the default read preference for the client (excluding tags). 指定客户端的默认读取首选项(不包括标记)。See read preference for more information.有关更多信息,请参阅读取偏好
readPreferenceTagscomma-separated key:value pairs, for example, "dc:ny,rack:1" and "dc:ny can be specified multiple times, each instance of this key is a separate tag set逗号分隔的键:值对,例如"dc:ny,rack:1""dc:ny可以指定多次,该键的每个实例都是一个单独的标记集nullSpecifies the default read preference tags for the client. 指定客户端的默认读取首选项标记。This option is valid only if the read preference mode is not primary.只有当读取首选项模式不是主模式时,此选项才有效。
The driver uses the order of the tags in the URI as the order for the read preference. 驱动程序使用URI中标记的顺序作为读取首选项的顺序。
replicaSetstringnullSpecifies the name of the replica set to connect to.指定要连接到的复制副本集的名称。
retryReadsbooleantrueEnables retryable reads.启用可重试读取。
retryWritesbooleantrueEnables retryable writes.启用可重试写入。
serverSelectionTimeoutMSnon-negative integer非负整数30000Specifies the timeout, in milliseconds, to block for server selection before raising an error.指定在引发错误之前阻止服务器选择的超时(以毫秒为单位)。
serverSelectionTryOncebooleantrueSpecifies to scan the topology only once after a server selection failure instead of repeatedly until the server selection times out.指定在服务器选择失败后只扫描拓扑一次,而不是重复扫描,直到服务器选择超时为止。
socketTimeoutMSnon-negative integer非负整数0Specifies the amount of time, in milliseconds, spent attempting to send or receive on a socket before timing out. 指定超时前尝试在套接字上发送或接收所花费的时间(以毫秒为单位)。Specifying 0 means no timeout.指定0表示没有超时。
srvMaxHostsnon-negative integer非负整数0Specifies the maximum number of SRV results to randomly select when initially populating the seedlist or, during SRV polling, adding new hosts to the topology.指定在最初填充种子列表时或在SRV轮询期间向拓扑添加新主机时要随机选择的SRV结果的最大数目。
srvServiceNamea valid SRV service name according to 根据RFC 6335的有效SRV服务名称"mongodb"Specifies the service name to use for SRV lookup in initial DNS seedlist discovery.指定在初始DNS种子列表发现中用于SRV查找的服务名称。
sslbooleanfalseThe ssl is an alias for the tls option.ssltls选项的别名。
timeoutMSnon-negative integer非负整数unset (feature is not enabled by default)unset(默认情况下未启用该功能)Specifies the timeout, in milliseconds, for the full execution of an operation.指定操作完全执行的超时时间(以毫秒为单位)。
tlsbooleanfalseSpecifies whether TLS is required for connections to the server. 指定连接到服务器是否需要TLS。Using a srvServiceName of "mongodb+srv", or specifying other tls prefixed options will default tls to true.使用"mongodb+srv"srvServiceName,或指定其他以tls为前缀的选项,将默认tlstrue
tlsAllowInvalidCertificatesbooleanfalseSpecifies whether the driver should error when the server’s TLS certificate is invalid.指定当服务器的TLS证书无效时,驱动程序是否应该出错。
tlsAllowInvalidHostnamesbooleanfalseSpecifies whether the driver should error when there is a mismatch between the server’s hostname and the hostname specified by the TLS certificate.指定当服务器的主机名与TLS证书指定的主机名不匹配时,驱动程序是否应该出错。
tlsCAFilestringnullSpecifies the path to a file with either a single or bundle of certificate authorities to trust when making a TLS connection.指定在建立TLS连接时要信任的具有单个或多个证书颁发机构的文件的路径。
tlsCertificateKeyFilestringnullSpecifies the path to the client certificate file or the client private key file. 指定客户端证书文件或客户端私钥文件的路径。If you need both, you must concatenate the files.如果两者都需要,则必须将文件连接起来。
tlsCertificateKeyFilePasswordstringnullSpecifies the password to decrypt the client private key to be used for TLS connections.指定用于解密TLS连接所使用的客户端私钥的密码。
tlsInsecurebooleanfalseSpecifies to relax TLS constraints as much as possible, such as allowing invalid certificates or hostname mismatches.指定尽可能放宽TLS约束,例如允许无效证书或主机名不匹配。
wnon-negative integer or string非负整数或字符串nullSpecifies the default write concern "w" field for the client.指定客户端的默认写入关注"w"字段。
waitQueueTimeoutMSnon-negative integer非负整数0Specifies the amount of time, in milliseconds, spent attempting to check out a connection from a server's connection pool before timing out.指定超时前尝试从服务器的连接池签出连接所花费的时间(以毫秒为单位)。
wTimeoutMSnon-negative integer非负整数nullSpecifies the default write concern "wtimeout" field for the client.指定客户端的默认写入关注"wtimeout"字段。
zlibCompressionLevelinteger between -1 and 9 (inclusive)-19之间(包含-19)的整型数-1Specifies the level of compression when using zlib to compress wire protocol messages. 指定使用zlib压缩有线协议消息时的压缩级别。-1 signifies the default level, 0 signifies no compression, 1 signifies the fastest speed, and 9 signifies the best compression. -1表示默认级别,0表示无压缩,1表示最快速度,9表示最佳压缩。See Network Compression for more information.有关详细信息,请参阅网络压缩