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分配给与会话相关联的命令。Importantmongosh 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:mongo
shell文档,请参阅相应MongoDB Server版本的文档:ThestartSession()
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()
enablescausalConsistency
by default.Mongo.startSession()
默认启用causalConsistency
。
After starting a session, you cannot modify its启动会话后,无法修改其causalConsistency
setting.causalConsistency
设置。NoteThe 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 themode
field and the optionaltags
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 forMongo.startSession()
.--retryWrites
选项启动mongosh
,则默认情况下会为Mongo.startSession()
启用retryWrite
。
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());