Definition
- Mongo.startSession(<options>)
- Starts a session for the connection. - mongoshassigns the session ID to commands associated with the session.- Important- mongosh Method- This page documents a - mongoshmethod. This is not the documentation for database commands or language-specific drivers, such as Node.js.- For the database command, see the - startSessioncommand.- For MongoDB API drivers, refer to the language-specific MongoDB driver documentation. - A session can only be used with the - MongoClientobject that created the session. A single session cannot be used concurrently. Operations that use a single session must be run sequentially.- The - startSession()method can take a document with session options. The options available are:FieldDescription- causalConsistency - Boolean. Enables or disables causal consistency for the session. - Mongo.startSession()enables- causalConsistencyby default. Mutually exclusive with- snapshot.- After starting a session, you cannot modify its - causalConsistencysetting.- The session may have causal consistency enabled even though the - Mongo()connection object may have causal consistency disabled or vice versa. To set causal consistency on the connection object, see- Mongo.setCausalConsistency().- readConcern - Document. Specifies the read concern. - To modify the setting after starting a session, see - Session.getOptions().setReadConcern().- readPreference - Document. Specifies the read preference. - The readPreference document contains the - modefield and the optional- tagsfield:- { mode: <string>, tags: <array> } - To modify the setting after starting a session, see - Session.getOptions().setReadPreference().- retryWrites - Boolean. Enables or disables the ability to retry writes upon encountering transient network errors. - If you start - mongoshwith the- --retryWritesoption,- retryWritesis enabled by default for- Mongo.startSession().- After starting a session, you cannot modify its - retryWritessetting.- snapshot - Boolean. Enables snapshot reads for the session for MongoDB 5.0+ deployments. Mutually exclusive with - causalConsistency.- writeConcern - Document. Specifies the write concern. - To modify the setting after starting a session, see - Session.getOptions().setWriteConcern().
Compatibility
This method is available in deployments hosted in the following environments:
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud 
Note
This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.
- MongoDB Enterprise: The subscription-based, self-managed version of MongoDB 
- MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB 
Examples
The following starts a session with causal consistency and retryable
writes enabled on the Mongo() connection object associated with
mongosh's global db variable:
db = db.getMongo().startSession({retryWrites: true, causalConsistency: true}).getDatabase(db.getName());