Mongo.startSession()
Definition
New in version 3.6.
Starts a session for the connection. Themongo
shell assigns the session id tocommands associated with the session.
The startSession()
method can take a document withsession options. The options available are:
FieldDescriptioncausalConsistencyBoolean. Enables or disables causal consistency for the session.Mongo.startSession()
enables causalConsistency
by default.
After starting a session, you cannot modify itscausalConsistency
setting.
Note
The session may have causal consistency enabled eventhough the Mongo
connection object may havecausal consistency disabled or vice versa. To set causalconsistency on the connection object, seeMongo.setCausalConsistency()
.
readConcernDocument. Specifies the read concern.
To modify the setting after starting a session, seeSessions.getOptions().setReadConcern()
.readPreferenceDocument. Specifies the read preference.
The readPreference document contains the mode
field andthe optional tags
field:
- { mode: <string>, tags: <array> }
To modify the setting after starting a session, seeSessions.getOptions().setReadPreference()
.retryWritesBoolean. Enables or disables the ability to retry writes uponencountering transient network errors.
If you start the mongo
shell with the—retryWrites
option, retryWrites
is enabled bydefault for Mongo.startSession()
.
After starting a session, you cannot modify itsretryWrites
setting.writeConcernDocument. Specifies the write concern.
To modify the setting after starting a session, seeSessions.getOptions().setWriteConcern()
.
Examples
The following starts a session with causal consistency and retryablewrites enabled on the Mongo
connection object associated withthe mongo
shell’s global db
variable:
- db = db.getMongo().startSession({retryWrites: true, causalConsistency: true}).getDatabase(db.getName());