Mongo.startSession()

On this page本页内容

Definition定义

Mongo.startSession(<options>)

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

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

Field字段Description描述
causalConsistency

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

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

Note注意

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()启用retryWites

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

writeConcern

Document. 文件Specifies the write concern.指定写入问题

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

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());
←  Mongo.setReadPref()Mongo.watch() →