Docs HomeMongoDB Manual

Retryable Reads可重试读取

Retryable reads allow MongoDB drivers to automatically retry certain read operations a single time if they encounter certain network or server errors.可重试读取允许MongoDB驱动程序在遇到某些网络或服务器错误时自动重试某些读取操作。

Prerequisites先决条件

Minimum Driver Version最低驱动程序版本

Official MongoDB drivers compatible with MongoDB Server 4.2 and later support retryable reads.与MongoDB Server 4.2及更高版本兼容的官方MongoDB驱动程序支持可重试读取。

For more information on official MongoDB drivers, see MongoDB Drivers.有关MongoDB官方驱动程序的更多信息,请参阅MongoDB驱动程序

Minimum Server Version最低服务器版本
Drivers can only retry read operations if connected to MongoDB Server 3.6 or later.只有连接到MongoDB Server 3.6或更高版本,驱动程序才能重试读取操作。

Enabling Retryable Reads启用可重试读取

Official MongoDB drivers compatible with MongoDB Server 4.2 and later enable retryable reads by default. 与MongoDB Server 4.2及更高版本兼容的官方MongoDB驱动程序默认启用可重试读取。To explicitly disable retryable reads, specify retryReads=false in the connection string for the deployment.若要显式禁用可重试读取,请在部署的连接字符串中指定retryReads=false

mongosh does not support retryable reads.不支持可重试读取。

Retryable Read Operations可重试读取操作

MongoDB drivers support retrying the following read operations. MongoDB驱动程序支持重试以下读取操作。The list references a generic description of each method. 该列表引用了每个方法的通用描述。For specific syntax and usage, defer to the driver documentation for that method.有关具体的语法和用法,请参阅该方法的驱动程序文档。

Methods方法Descriptions描述
Collection.aggregate
Collection.count
Collection.countDocuments
Collection.distinct
Collection.estimatedDocumentCount
Collection.find
Database.aggregate
For Collection.aggregate and Database.aggregate, drivers can only retry aggregation pipelines which do not include write stages, such as $out or $merge. 对于Collection.aggregateDatabase.aggregate,驱动程序只能重试不包括写入阶段(如$out$merge)的聚合管道。
CRUD API Read OperationsCRUD API读取操作
Collection.watch
Database.watch
MongoClient.watch
Change Stream Operations更改流操作
MongoClient.listDatabases
Database.listCollections
Collection.listIndexes
Enumeration Operations枚举操作
GridFS Operations backed by Collection.find (e.g. GridFSBucket.openDownloadStream)支持的GridFS操作(例如GridFSBucket.openDownloadStreamGridFS File Download OperationsCollection.findGridFS文件下载操作

MongoDB drivers may include retryable support for other operations, such as helper methods or methods that wrap a retryable read operation. MongoDB驱动程序可能包括对其他操作的可重试支持,例如辅助方法或包装可重试读取操作的方法。Defer to the driver documentation to determine whether a method explicitly supports retryable reads.根据驱动程序文档来确定方法是否明确支持可重试读取。

Tip

See also: 另请参阅:

Retryable Read Specification: Supported Read Operations可重试读取规范:支持的读取操作

Unsupported Read Operations不支持的读取操作

The following operations do not support retryable reads:以下操作不支持可重试读取:

  • db.collection.mapReduce()
  • getMore
  • Any read command passed to a generic Database.runCommand helper, which is agnostic about read or write commands.传递给通用Database.runCommand帮助程序的任何读取命令,该帮助程序与读取或写入命令无关。

Behavior行为

Persistent Network Errors持续的网络错误

MongoDB retryable reads make only one retry attempt. MongoDB可重试读取只进行一次重试尝试。This helps address transient network errors or replica set elections, but not persistent network errors.这有助于解决暂时的网络错误或副本集选举,但不会解决持久的网络错误。

Failover Period故障转移周期

The driver performs server selection using the read command's original read preference before retrying the read operation. 在重试读取操作之前,驱动程序使用读取命令的原始读取首选项执行服务器选择If the driver cannot select a server for the retry attempt using the original read preference, the driver returns the original error.如果驱动程序无法使用原始读取首选项为重试尝试选择服务器,则驱动程序将返回原始错误。

The drivers wait serverSelectionTimeoutMS milliseconds before performing server selection. 在执行服务器选择之前,驱动程序等待serverSelectionTimeoutMS毫秒。Retryable reads do not address instances where no eligible servers exist after waiting serverSelectionTimeoutMS.在等待serverSelectionTimeoutMS之后,如果不存在符合条件的服务器,则可重试读取不会寻址实例。