Database Manual / Reference / mongosh Methods / Connections

Session会话

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

Updates the cluster time tracked by the session.更新会话跟踪的群集时间。

This method has the following parameters:此方法具有以下参数:

Session.advanceClusterTime( {
clusterTime: <timestamp>,
signature:
{ hash: <BinData>, keyId: <NumberLong> }
} )
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. For details, see Session.withTransaction().在事务中运行指定的lambda函数。有关详细信息,请参阅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());