Database Manual / Reference / mongosh Methods / Cursors

cursor.readConcern() (mongosh method方法)

Definition定义

cursor.readConcern(level)

Important

mongosh Method方法

This page documents a mongosh method. This is not the documentation for a language-specific driver, such as Node.js.本页记录了一种mongosh方法。这不是针对特定语言驱动程序(如Node.js)的文档。

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档

Specify a read concern for the db.collection.find() method.db.collection.find()方法指定一个读取关注

The readConcern() method has the following form:readConcern()方法具有以下形式:

db.collection.find().readConcern(<level>)

The readConcern() method has the following parameter:readConcern()方法有以下参数:

Parameter参数Type类型Description描述
levelstring字符串

Read concern读取关注 level.水平。

Possible read concern levels are:可能的读取关注级别包括:

  • "local". This is the default read concern level for read operations against the primary and secondaries.。这是针对主要和次要读取操作的默认读取关注级别。
  • "available". Available for read operations against the primary and secondaries. 。可用于对初级和次级进行读取操作。"available" behaves the same as "local" against the primary and non-sharded secondaries. The query returns the instance's most recent data.对初级和非分片次级的行为与"local"相同。查询返回实例的最新数据。
  • "majority". Available for replica sets that use WiredTiger storage engine.。适用于使用WiredTiger存储引擎的副本集。
  • "linearizable". Available for read operations on the primary only.。仅适用于primary上的读取操作。
  • "snapshot". Available for multi-document transactions and certain read operations outside of multi-document transactions.。可用于多文档事务和多文档事务之外的某些读取操作。

For more formation on the read concern levels, see Read Concern Levels.有关读取关注级别的更多信息,请参阅读取关注级别

Compatibility兼容性

This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Note

This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Considerations注意事项

Read Your Own Writes阅读自己的文章

You can use causally consistent sessions to read your own writes, if the writes request acknowledgment.如果写入请求确认,您可以使用因果一致的会话来读取自己的写入。

Linearizable Read Concern Performance线性化读取关注性能

When specifying linearizable read concern, always use maxTimeMS() in case a majority of data bearing members are unavailable.在指定可线性化的读取关注时,如果大多数数据承载成员不可用,请始终使用maxTimeMS()

db.restaurants.find( { _id: 5 } ).readConcern("linearizable").maxTimeMS(10000)