This section explains the MongoDB connection and authentication options supported by the Node.js driver that you can set within a 本节解释了Node.js驱动程序支持的MongoDB连接和身份验证选项,您可以在MongoClientOptions instance.MongoClientOptions实例中设置这些选项。
For more information on setting connection options directly in a connection string, see Connection Strings in the MongoDB Server manual.有关直接在连接字符串中设置连接选项的更多信息,请参阅MongoDB Server手册中的连接字符串。
appName | string | null | appName value to MongoDB when establishing the connection. This value is recorded in the log file, the slow query logs, and profile collections. |
authMechanism | string | null | SCRAM-SHA-1 or SCRAM-SHA-256 depending on the server version. SCRAM-SHA-1或SCRAM-SHA-256。 |
authMechanismProperties | null | ||
authSource | string | null | |
autoSelectFamily | boolean | true | true, the socket attempts to connect to IPv6 and IPv4 addresses until a connection is established. If available, the driver will select the first IPv6 address.true,套接字将尝试连接到IPv6和IPv4地址,直到建立连接。如果可用,驱动程序将选择第一个IPv6地址。 |
autoSelectFamilyAttemptTimeout | null | autoSelectFamily option. If set to a positive integer less than 10, the value 10 is used instead.autoSelectFamily选项时,在尝试下一个地址之前等待连接尝试完成的时间量(以毫秒为单位)。如果设置为小于10的正整数,则使用值10。 | |
compressors | null | ||
connectTimeoutMS | 30000 | 0 means your application sets an infinite socket timeout when establishing a socket.0意味着您的应用程序在建立套接字时设置了无限套接字超时。 | |
directConnection | boolean | false | |
enableUtf8Validation | boolean | true |
|
heartbeatFrequencyMS | null | ||
loadBalanced | boolean | null | |
localThresholdMS | 15 | 0 means no wait, meaning the fastest available server. | |
maxIdleTimeMS | 0 | 0 means no minimum.0表示没有最小值。 | |
maxPoolSize | 100 | ||
maxConnecting | 2 | ||
maxStalenessSeconds | null | -1 means no maximum.-1表示没有最大值。 | |
minPoolSize | 0 | ||
proxyHost | string | null | |
proxyPort | null | proxyHost option, the value of this option defaults to 1080.proxyHost选项,则此选项的值默认为1080。 | |
proxyUsername | string | null | |
proxyPassword | string | null | |
readConcernLevel | string | null | |
readPreference | string | "primary" | |
readPreferenceTags | null |
| |
replicaSet | string | null | |
retryReads | boolean | true | |
retryWrites | boolean | true | |
serverMonitoringMode | auto, stream, poll | auto | auto, the monitoring mode is determined by the environment in which the driver is running. auto时,监控模式由驱动程序运行的环境决定。 |
serverSelectionTimeoutMS | 30000 | ||
socketTimeoutMS | 0 | 0 means your application sets an infinite socket timeout when establishing a socket.0意味着您的应用程序在建立套接字时设置了无限套接字超时。 | |
srvMaxHosts | 0 | ||
srvServiceName | "mongodb" | ||
ssl | boolean | false | ssl is an alias for the tls option.ssl是tls选项的别名。 |
tls | boolean | false | srvServiceName of "mongodb+srv", or specifying other tls-prefixed options implicitly sets the value of tls to true."mongodb+srv"的srvServiceName,或指定其他以tls为前缀的选项,隐式地将tls的值设置为true。 |
tlsAllowInvalidCertificates | boolean | false | true for testing purposes only.true仅用于测试目的。 |
tlsAllowInvalidHostnames | boolean | false | true for testing purposes only.true仅用于测试目的。 |
tlsCAFile | string | null | |
tlsCertificateKeyFile | string | null | |
tlsCertificateKeyFilePassword | string | null | |
tlsInsecure | boolean | false | true for testing purposes only.true仅用于测试目的。 |
waitQueueTimeoutMS | 0 |
| |
zlibCompressionLevel | -1 and 9 (inclusive)-1和9(含)之间的整数 | -1 | -1 signifies the default level, 0 signifies no compression, 1 signifies the fastest speed, and 9 signifies the best compression. -1表示默认级别,0表示无压缩,1表示最快速度,9表示最佳压缩。 |
Connection Time Out Options连接超时选项
connectTimeoutMS |
|
socketTimeoutMS | socketTimeoutMS |
maxTimeMS | maxTimeMSmaxTimeMS only to an individual operation or to a cursor.maxTimeMS传递给单个操作或游标。 |
To specify the optional settings for your 要为MongoClient, declare one or more available settings in the options object of the constructor as follows:MongoClient指定可选设置,请在构造函数的options对象中声明一个或多个可用设置,如下所示:
const client = new MongoClient(uri, {
connectTimeoutMS: <integer value>,
socketTimeoutMS: <integer value>
});
To see all the available settings, see the MongoClientOptions API Documentation.要查看所有可用的设置,请参阅MongoClientOptions API文档。
To specify 要指定maxTimeMS, pass in the maxTimeMS method as an option with a timeout specification to an operation that returns a Cursor:maxTimeMS,请将maxTimeMS方法作为带有超时规范的选项传递给返回Cursor的操作:
const Cursor = collection.distinct('my-key', { maxTimeMS: 50 });Close Sockets After Connection连接后关闭套接字
If you experience unexpected network behavior or if a MongoDB process fails with an error, you might not receive confirmation that the driver correctly closed the corresponding socket.如果您遇到意外的网络行为,或者MongoDB进程因错误而失败,您可能不会收到驱动程序正确关闭相应套接字的确认。
To make sure that the driver correctly closes the socket in these cases, set the 为确保在这些情况下驱动程序正确关闭插座,请设置socketTimeoutMS option. When a MongoDB process times out, the driver will close the socket. socketTimeoutMS选项。当MongoDB进程超时时,驱动程序将关闭套接字。We recommend that you select a value for 我们建议您为socketTimeoutMS that is two to three times longer than the expected duration of the slowest operation that your application executes.socketTimeoutMS选择一个值,该值比应用程序执行的最慢操作的预期持续时间长两到三倍。
Prevent Long-Running Operations From Slowing Down the Server防止长时间运行的操作减慢服务器速度
You can prevent long-running operations from slowing down the server by specifying a timeout value. You can chain the 您可以通过指定超时值来防止长时间运行的操作减慢服务器的速度。您可以将maxTimeMS() method to an operation that returns a Cursor to set a timeout on a specific action.maxTimeMS()方法链接到一个操作,该操作返回一个游标以设置特定操作的超时。
The following example shows how you can chain the 以下示例显示了如何将maxTimeMS() method to an operation that returns a Cursor:maxTimeMS()方法链接到返回Cursor的操作:
// Execute a find command执行查找命令
await collection
.find({ $where: "sleep(100) || true" })
.maxTimeMS(50);keepAlive Connection OptionkeepAlive连接选项
The keepAlive connection option specifies whether to enable Transmission Control Protocol (TCP) keepalives on a TCP socket. keepAlive连接选项指定是否在TCP套接字上启用传输控制协议(TCP)保活。If you enable keepalives, the driver checks whether the connection is active by sending periodic pings to your MongoDB deployment. This functionality works only if your operating system supports the 如果启用SO_KEEPALIVE socket option.keepalives,驱动程序会通过向MongoDB部署发送定期ping来检查连接是否处于活动状态。仅当您的操作系统支持SO_KEEPALIVE套接字选项时,此功能才有效。
The keepAliveInitialDelay option specifies the number of milliseconds that the driver waits before initiating a keepalive.keepAliveInitialDelay选项指定驱动程序在启动保活之前等待的毫秒数。
The 5.3 driver version release deprecated these options. Starting in version 6.0 of the driver, the 5.3驱动程序版本已弃用这些选项。从驱动程序的6.0版本开始,keepAlive option is permanently set to true, and the keepAliveInitialDelay is set to 300000 milliseconds (300 seconds).keepAlive选项永久设置为true,keepAliveInitialDelay设置为300000毫秒(300秒)。
Warning
If your firewall ignores or drops the keepalive messages, you might not be able to identify dropped connections.如果您的防火墙忽略或丢弃了保活消息,您可能无法识别丢失的连接。
Additional Information附加信息
To learn more about connection options that you can set within a 要了解有关可以在MongoClientOptions instance, see MongoClientOptions in the API Documentation.MongoClientOptions实例中设置的连接选项的更多信息,请参阅API文档中的MongoClientOptions。