定义
startSessionThe
startSessioncommand starts a new logical session for a sequence of operations.提示
In
mongosh, this command can also be run through theMongo.startSession()helper method.Helper methods are convenient for
mongoshusers, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.
兼容性
此命令可用于以下环境中托管的部署:
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
注意
This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
该命令具有以下语法:
db.runCommand( { startSession: 1 } )
To run startSession, use the db.runCommand( { <command> } ) method.
db.runCommand( { startSession: 1 } )
重要
featureCompatibilityVersion must be 3.6 or greater to use the startSession command.
行为
If the deployment enforces authentication/authorization, you must be authenticated to run the startSession command. The user who runs startSession owns the created session, and only that user can use the session.
如果部署不强制执行身份验证/授权,则创建的会话没有所有者,任何用户可以在任何连接上使用该会话。 如果用户对不强制执行身份验证/授权的部署进行身份验证并创建会话,则用户拥有该会话。 但是,任何连接上的任何用户都可以使用该会话。
如果部署在没有停机的情况下过渡到身份验证,则无法使用任何没有所有者的会话。
会话只能与创建会话的 MongoClient 对象一起使用。单个会话不能同时使用。使用单个会话的操作必须按顺序运行。
输出
In addition to the status and operation time of the command, the startSession returns the following session specific information:
字段 | 类型 | 说明 | |
|---|---|---|---|
| 文档 | 包含会话标识符的 16 字节通用唯一标识符 (UUID) 部分的文档。 会话标识符由该 UUID 与经过身份验证的用户档案的哈希值连接组成。 | |
| 数字 | 自客户端上次使用以来会话在过期之前保持活动状态的时间量(以分钟为单位)。 默认情况下,会话的超时时间为 30 分钟。 要更改该值,请在启动 |