Docs Menu

Session

On this page

  • Definition
  • Example
Session

New in version 3.6.

The Session object for the connection in mongosh. To instantiate a session for the connection in mongosh, see 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.
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().
Starts a multi-document transaction for the session. For details, see Session.startTransaction().
Commits the session's transaction. For details, see Session.commitTransaction().
Aborts the session's transaction. For details, see Session.abortTransaction().

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.

var session = db.getMongo().startSession();
db = session.getDatabase(db.getName());

On this page

Give Feedback
MongoDB logo
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.