Database Manual / Reference / mongosh Methods / Connections

Mongo.startSession() (mongosh method方法)

Definition定义

Mongo.startSession(<options>)

Starts a session for the connection. mongosh assigns the session ID to commands associated with the session.启动连接会话mongosh将会话ID分配给与会话关联的命令。

Important

mongosh Method方法

This page documents a mongosh method. This is not the documentation for database commands or language-specific drivers, such as Node.js.本页记录了一种mongosh方法。这不是数据库命令或特定语言驱动程序(如Node.js)的文档。

For the database command, see the startSession command.有关数据库命令,请参阅startSession命令。

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

A session can only be used with the MongoClient object that created the session. A single session cannot be used concurrently. Operations that use a single session must be run sequentially.会话只能与创建会话的MongoClient对象一起使用。单个会话不能同时使用。使用单个会话的操作必须按顺序运行。

The startSession() method can take a document with session options. The options available are:startSession()方法可以接收带有会话选项的文档。可用的选项有:

Field字段Description描述
causalConsistency

Boolean. Enables or disables causal consistency for the session. 布尔值。启用或禁用会话的因果一致性Mongo.startSession() enables causalConsistency by default. Mutually exclusive with snapshot.默认情况下启用causalConsistency。与snapshot相互排斥。

After starting a session, you cannot modify its causalConsistency setting.启动会话后,您无法修改其causalConsistency设置。

The session may have causal consistency enabled even though the Mongo() connection object may have causal consistency disabled or vice versa. 即使Mongo()连接对象可能禁用因果一致性,会话也可能启用因果一致性;反之亦然。To set causal consistency on the connection object, see Mongo.setCausalConsistency().要在连接对象上设置因果一致性,请参阅Mongo.setCausalConsistency()

readConcern

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

To modify the setting after starting a session, see Session.getOptions().setReadConcern().要在启动会话后修改设置,请参阅Session.getOptions().setReadConcern()

readPreference

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

The readPreference document contains the mode field and the optional tags field:readPreference文档包含mode字段和可选tags 字段:

{ mode: <string>, tags: <array> }

To modify the setting after starting a session, see Session.getOptions().setReadPreference().要在启动会话后修改设置,请参阅Session.getOptions().setReadPreference()

retryWrites

Boolean. Enables or disables the ability to retry writes upon encountering transient network errors.布尔值。启用或禁用在遇到瞬态网络错误时重试写入的能力。

If you start mongosh with the --retryWrites option, retryWrites is enabled by default for Mongo.startSession().如果您使用--retryWrites选项启动mongosh,则默认情况下Mongo.startSession()会启用retryWrites

After starting a session, you cannot modify its retryWrites setting.启动会话后,您无法修改其retryWrites设置。

snapshotBoolean. Enables snapshot reads for the session for MongoDB 5.0+ deployments. Mutually exclusive with causalConsistency.布尔值。为MongoDB 5.0+部署启用会话快照读取。与causalConsistency相互排斥。
writeConcern

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

To modify the setting after starting a session, see Session.getOptions().setWriteConcern().要在启动会话后修改设置,请参阅Session.getOptions().setWriteConcern()

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的源代码可用、免费使用和自我管理版本

Examples示例

The following starts a session with causal consistency and retryable writes enabled on the Mongo() connection object associated with mongosh's global db variable:下面开始一个会话,在与mongosh的全局db变量关联的Mongo()连接对象上启用因果一致性和可重试写入:

db = db.getMongo().startSession({retryWrites: true, causalConsistency: true}).getDatabase(db.getName());