Definition定义
Mongo.startSession(<options>)Starts a session for the connection.启动连接会话。mongoshassigns the session ID to commands associated with the session.mongosh将会话ID分配给与会话关联的命令。Important
mongosh
Method方法This page documents a本页记录了一种mongoshmethod. 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有关数据库命令,请参阅startSessioncommand.startSession命令。For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档。A session can only be used with the会话只能与创建会话的MongoClientobject that created the session. A single session cannot be used concurrently. Operations that use a single session must be run sequentially.MongoClient对象一起使用。单个会话不能同时使用。使用单个会话的操作必须按顺序运行。ThestartSession()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默认情况下启用causalConsistencyby default. Mutually exclusive withsnapshot.causalConsistency。与snapshot相互排斥。After starting a session, you cannot modify its启动会话后,您无法修改其causalConsistencysetting.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()。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 themodefield and the optionaltagsfield: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如果您使用mongoshwith the--retryWritesoption,retryWritesis enabled by default forMongo.startSession().--retryWrites选项启动mongosh,则默认情况下Mongo.startSession()会启用retryWrites。After starting a session, you cannot modify its启动会话后,您无法修改其retryWritessetting.retryWrites设置。snapshotBoolean. Enables snapshot reads for the session for MongoDB 5.0+ deployments. Mutually exclusive with布尔值。为MongoDB 5.0+部署启用会话快照读取。与causalConsistency.causalConsistency相互排斥。writeConcernDocument. 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());