Session

On this page本页内容

Definition定义

Session

The Session object for the connection in mongosh. mongosh中连接的会话对象。To instantiate a session for the connection in mongosh, see Mongo.startSession(). 要在mongosh中为连接实例化会话,请参阅Mongo.startSession()For more information on sessions, see Client Sessions and Causal Consistency Guarantees.有关会话的更多信息,请参阅客户端会话和因果一致性保证

Method方法Description描述
Session.getDatabase(<database>)Access the specified database from the session in mongosh.mongosh中的会话访问指定的数据库。
Session.advanceClusterTime({ clusterTime: <timestamp>, signature: { hash: <BinData>, keyId: <NumberLong> } })Updates the cluster time tracked by the session.更新会话跟踪的群集时间。
Session.advanceOperationTime(<timestamp>)Updates the operation time.更新操作时间。
Session.endSession()Ends the session.结束会话。
Session.hasEnded()Returns a boolean that specifies whether the session has ended.返回一个布尔值,指定会话是否已结束。
Session.getClusterTime()Returns the most recent cluster time as seen by the session. 返回会话看到的最近群集时间。Applicable for replica sets and sharded clusters only.仅适用于复制集和分片群集。
Session.getOperationTime()Returns the timestamp of the last acknowledged operation for the session.返回会话的上次确认操作的时间戳。
Session.getOptions()Access the options for the session. 访问会话的选项。For the available options, see SessionOptions().有关可用选项,请参阅SessionOptions()
Session.startTransaction()Starts a multi-document transaction for the session. 启动会话的多文档事务。For details, see Session.startTransaction().有关详细信息,请参阅Session.startTransaction()
Session.commitTransaction()Commits the session's transaction. 提交会话的事务。For details, see Session.commitTransaction().有关详细信息,请参阅Session.commitTransaction()
Session.abortTransaction()Aborts the session's transaction. 中止会话的事务。For details, see Session.abortTransaction().有关详细信息,请参阅Session.abortTransaction()

Example示例

The following example starts a session on the Mongo() connection object associated with mongosh's global db variable, and then uses the Session.getDatabase() method to retrieve the database object associated with the session.以下示例在与mongosh的全局db变量关联的Mongo()连接对象上启动会话,然后使用Session.getDatabase()方法检索与会话关联的数据库对象。

var session = db.getMongo().startSession();
db = session.getDatabase(db.getName());
←  Mongo.watch()Session.abortTransaction() →