Session
On this page
This version of the documentation is archived and no longer supported. View the current documentation to learn how to upgrade your version of MongoDB server.
Definition
Session
New in version 3.6.
The Session object for the connection in
mongosh
. To instantiate a session for the connection inmongosh
, seeMongo.startSession()
. For more information on sessions, see Client Sessions and Causal Consistency Guarantees.MethodDescriptionAccess the specified database from the session inmongosh
.Updates the cluster time tracked by the session.Updates the operation time.Ends the session.Returns a boolean that specifies whether the session has ended.Returns the most recent cluster time as seen by the session. Applicable for replica sets and sharded clusters only.Returns the timestamp of the last acknowledged operation for the session.Access the options for the session. For the available options, seeSessionOptions()
.Starts a multi-document transaction for the session. For details, seeSession.startTransaction()
.Commits the session's transaction. For details, seeSession.commitTransaction()
.Aborts the session's transaction. For details, seeSession.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.
var session = db.getMongo().startSession(); db = session.getDatabase(db.getName());