Docs HomeMongoDB Manual

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分配给与会话相关联的命令。

Important

mongosh Method

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

For the legacy mongo shell documentation, refer to the documentation for the corresponding MongoDB Server release:对于遗留的mongoshell文档,请参阅相应MongoDB Server版本的文档:

mongo shell v4.4

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

Field字段Description描述
causalConsistencyBoolean. 布尔值。Enables or disables causal consistency for the session. Mongo.startSession() enables causalConsistency by default.启用或禁用会话的因果一致性Mongo.startSession()默认启用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()
readConcernDocument. 文档。Specifies the read concern.指定读取关注
To modify the setting after starting a session, see Session.getOptions().setReadConcern(). 要在启动会话后修改设置,请参阅Session.getOptions().setReadConcern()
readPreferenceDocument. 文档。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()
retryWritesBoolean. 布尔值。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()启用retryWrite
After starting a session, you cannot modify its retryWrites setting. 启动会话后,无法修改其retryWrites设置。
writeConcernDocument. 文档。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());