定义
cursor.readConcern(level)重要
mongosh 方法
This page documents a
mongoshmethod. This is not the documentation for a language-specific driver, such as Node.js.如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。
为 方法指定
db.collection.find()读关注(read concern) 。The
readConcern()method has the following form:db.collection.find().readConcern(<level>) The
readConcern()method has the following parameter:Parameter类型说明level字符串
可能的读关注级别是:
"local"这是针对主节点和从节点的读操作的默认读关注级别。"available"。可用于对主节点和从节点执行读取操作。对于主节点和非分片从节点,"available"行为与"local"相同。查询会返回实例的最新数据。"majority"。适用于使用 WiredTiger 存储引擎的副本集。"linearizable"。仅适用于primary的读取操作。"snapshot"。可用于多文档事务以及多文档事务之外的某些读取操作。
有关读关注级别的更多信息,请参阅读关注级别。
兼容性
此方法可用于以下环境中托管的部署:
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
注意
This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
Considerations
读取自己的写入
如果写入请求确认,您可以使用因果一致的会话来读取您自己的写入。
线性化读关注(read concern)性能
指定linearizable read concern时,请始终使用maxTimeMS() ,以防大多数数据承载节点不可用。
db.restaurants.find( { _id: 5 } ).readConcern("linearizable").maxTimeMS(10000)