Docs HomeMongoDB Manual

Session

On this page本页内容

Definition定义

Session

The Session object for the connection in mongosh. mongosh中连接的Session对象。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.有关会话的更多信息,请参阅客户端会话和因果一致性保证

General Session Methods常规会话方法

Method方法Description描述
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.getClusterTime() Returns the most recent cluster time as seen by the session. 返回会话看到的最近的群集时间。Applicable for replica sets and sharded clusters only.仅适用于复制集和分片集群。
Session.getDatabase(<database>) Access the specified database from the session in mongosh.mongosh中的会话访问指定的数据库。
Session.getOptions() Access the options for the session. For the available options, see SessionOptions().访问会话的选项。有关可用选项,请参阅SessionOptions()
Session.getOperationTime() Returns the timestamp of the last acknowledged operation for the session.返回会话最后一次确认操作的时间戳。
Session.hasEnded() Returns a boolean that specifies whether the session has ended.返回一个布尔值,用于指定会话是否已结束。

Session Methods for Transactions事务的会话方法

Method方法Description描述
Session.abortTransaction()Aborts the session's transaction. 中止会话的事务。For details, see Session.abortTransaction().有关详细信息,请参阅Session.abortTransaction()
Session.commitTransaction()Commits the session's transaction. For details, see Session.commitTransaction().提交会话的事务。有关详细信息,请参阅Session.commitTransaction()
Session.startTransaction()Starts a multi-document transaction for the session. 启动会话的多文档事务。For details, see Session.startTransaction().有关详细信息,请参阅Session.startTransaction()
Session.withTransaction()Runs a specified lambda function within a transaction. 在事务中运行指定的lambda函数。For details, see Session.withTransaction().有关详细信息,请参阅Session.withTransaction()

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());