Docs HomeMongoDB Manual

SessionOptions

On this page本页内容

Definition定义

SessionOptions

The options for a session in mongosh. mongoshsession选项。To access the SessionOptions() object, use the Session.getOptions() method.要访问SessionOptions()对象,请使用Session.getOptions()方法。

The session options available are:可用的会话选项包括:

Option选项Description描述
causalConsistencyBoolean. 布尔值。Enables or disables causal consistency for the session.启用或禁用会话的因果一致性
You can explicitly set the option when you start a session manually: 手动启动会话时,可以显式设置该选项:
  • Session = db.getMongo().startSession( { causalConsistency: true } )
readConcernDocument. 文档。Specifies the read concern.指定读取关注
In mongosh, you can set the option when you run Mongo.startSession(). mongosh中,您可以在运行Mongo.startSession()时设置该选项。You can also access the readConcern option via the following methods: 您也可以通过以下方法访问readConcern选项:
  • Session.getOptions().getReadConcern()
  • Session.getOptions().setReadConcern(<document>)
readPreferenceDocument. 文档。Specifies the read preference.指定读取首选项
In mongosh, you can set the option when you run Mongo.startSession(). mongosh中,您可以在运行Mongo.startSession()时设置该选项。You can also access the readPreference option via the following methods: 您也可以通过以下方法访问readPreference选项:
  • Session.getOptions().getReadPreference()
  • Session.getOptions().setReadPreference({ mode: <string>, tags: <array>})
retryWritesBoolean. 布尔值。Enables or disables the ability to retry writes upon encountering transient network errors, such as during failovers.启用或禁用在遇到瞬时网络错误时重试写入的功能,例如在故障切换期间。
To enable retry writes, start mongosh with the --retryWrites option.要启用重试写入,请使用--retryWrites选项启动mongosh
You can view whether retryWrites is enabled for a session via the following method: 您可以通过以下方法查看是否为会话启用了retryWrites
  • Session.getOptions().shouldRetryWrites()
writeConcernDocument. 文档。Specifies the write concern.指定写入关注
In mongosh, you can set the options when you run Mongo.startSession(). mongosh中,您可以在运行Mongo.startSession()时设置选项。You can also access the writeConcern option via the following methods: 您还可以通过以下方法访问writeConcern选项:
  • Session.getOptions().getWriteConcern()
  • Session.getOptions().setWriteConcern(<document>)

Verify which options are enabled for the session by running Session.getOptions().通过运行Session.getOptions()验证为会话启用了哪些选项。