Navigation
This is an upcoming (i.e. in progress) version of the manual.

Mongo.startSession()

On this page

Definition

Mongo.startSession(<options>)

New in version 3.6.

Starts a session for the connection. The mongo shell assigns the session id to commands associated with the session.

The startSession() method can take a document with session options. The options available are:

Field Description
causalConsistency

Boolean. Enables or disables causal consistency for the session. Mongo.startSession() enables causalConsistency by default.

After starting a session, you cannot modify its causalConsistency setting.

Note

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 Sessions.getOptions().setReadConcern().

readPreference

Document. Specifies the read preference.

The readPreference document contains the mode field and the optional tags field:

{ mode: <string>, tags: <array> }

To modify the setting after starting a session, see Sessions.getOptions().setReadPreference().

retryWrites

Boolean. Enables or disables the ability to retry writes upon encountering transient network errors.

If you start the mongo shell with the --retryWrites option, retryWrites is enabled by default for Mongo.startSession().

After starting a session, you cannot modify its retryWrites setting.

writeConcern

Document. Specifies the write concern.

To modify the setting after starting a session, see Sessions.getOptions().setWriteConcern().

Examples

The following starts a session with causal consistency and retryable writes enabled on the Mongo connection object associated with the mongo shell’s global db variable:

db = db.getMongo().startSession({retryWrites: true, causalConsistency: true}).getDatabase(db.getName());