Retryable Reads可重试的读

On this page本页内容

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.mongosh不支持可重试读取。

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)Collection.find支持的GridFS操作(例如GridFSBucket.openDownloadStreamGridFS File Download OperationsGridFS文件下载操作

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后不存在符合条件的服务器的实例。

←  Retryable WritesSQL to MongoDB Mapping Chart →