Database Manual / Reference / mongosh Methods / Connections

SessionOptions

Definition定义

SessionOptions

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

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

Option选项Description描述
causalConsistency

Boolean. 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 } )
readConcern

Document. Specifies the read concern.文件。指定读取关注

In mongosh, you can set the option when you run Mongo.startSession(). You can also access the readConcern option via the following methods:mongosh中,您可以在运行Mongo.startSession()时设置该选项。您还可以通过以下方法访问readConcern选项:

  • Session.getOptions().getReadConcern()
  • Session.getOptions().setReadConcern(<document>)
readPreference

Document. Specifies the read preference.文档。指定读取首选项

In mongosh, you can set the option when you run Mongo.startSession(). You can also access the readPreference option via the following methods:mongosh中,您可以在运行Mongo.startSession()时设置该选项。您还可以通过以下方法访问readPreference选项:

  • Session.getOptions().getReadPreference()
  • Session.getOptions().setReadPreference({ mode: <string>, tags: <array>})
retryWrites

Boolean. 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()
writeConcern

Document. Specifies the write concern.文件。指定写入关注

In mongosh, you can set the options when you run Mongo.startSession(). You can also access the writeConcern option via the following methods:mongosh中,您可以在运行Mongo.startSession()时设置选项。您还可以通过以下方法访问writeConcern选项:

  • Session.getOptions().getWriteConcern()
  • Session.getOptions().setWriteConcern(<document>)

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

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部署的完全托管服务
  • 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的源代码可用、免费使用和自我管理版本