cursor.readConcern()

On this page本页内容

Definition定义

cursor.readConcern(level)
Important重要
mongosh Method

This is a mongosh method. This is not the documentation for Node.js or other programming language specific driver methods.

In most cases, mongosh methods work the same way as the legacy mongo shell methods. However, some legacy methods are unavailable in mongosh.

For the legacy mongo shell documentation, refer to the documentation for the corresponding MongoDB Server release:

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. 对于主分区和非分片的次分区,其行为与"local"相同。The query returns the instance's most recent data.查询返回实例的最新数据。
  • "majority". Available for replica sets that use WiredTiger storage engine.适用于使用WiredTiger存储引擎的副本集。
  • "linearizable". Available for read operations on the primary only.仅适用于primary上的读取操作。

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

Considerations注意事项

Read Your Own Writes阅读自己的文章

Starting in MongoDB 3.6, you can use causally consistent sessions to read your own writes, if the writes request acknowledgement.从MongoDB 3.6开始,如果写入请求确认,您可以使用因果一致会话来读取自己的写入。

Prior to MongoDB 3.6, in order to read your own writes you must issue your write operation with { w: "majority" } write concern, and then issue your read operation with primary read preference, and either "majority" or "linearizable" read concern.在MongoDB 3.6之前,为了读取您自己的写操作,您必须使用{ w: "majority" }写关注点发出写操作,然后使用primary读首选项发出读操作,或者使用"majority""linearizable"读关注点。

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)
Tip提示
See also: 参阅:
←  cursor.pretty()cursor.readPref() →