定義
Mongo.startSession(<options>)Starts a session for the connection.
mongoshassigns the session ID to commands associated with the session.重要
mongosh メソッド
This page documents a
mongoshmethod. This is not the documentation for database commands or language-specific drivers, such as Node.js.データベースコマンドについては、
startSessionコマンドを参照してください。MongoDB API ドライバーについては、各言語の「MongoDB ドライバーのドキュメント」を参照してください。
セッションは、セッションを作成した
MongoClientオブジェクトでのみ使用できます。 1 つのセッションを同時に使用することはできません。 1 つのセッションを使用する操作は連続して実行する必要があります。The
startSession()method can take a document with session options. The options available are:フィールド説明因果整合性
Boolean. Enables or disables causal consistency for the session.
Mongo.startSession()enablescausalConsistencyby default. Mutually exclusive withsnapshot.セッションを開始した後は、その
causalConsistency設定を変更することはできません。Mongo()接続オブジェクトで因果整合性が無効になっている場合でも、セッションでは因果整合性が有効になっている場合や、 接続オブジェクトに因果整合性を設定するには、Mongo.setCausalConsistency()を参照してください。ReadConcern
ドキュメント。 読み取り保証 ( read concern) を指定します。
セッションを開始した後に設定を変更するには、
Session.getOptions().setReadConcern()を参照してください。readPreference
ドキュメント。 読み込み設定 ( read preference) を指定します。
readPreference ドキュメントには、
modeフィールドと任意のtagsフィールドが含まれています。{ mode: <string>, tags: <array> } セッションを開始した後に設定を変更するには、
Session.getOptions().setReadPreference()を参照してください。retryWrites
ブール値。 一時的なネットワークエラーが発生したときに書き込みを再試行する機能を有効または無効にします。
If you start
mongoshwith the--retryWritesoption,retryWritesis enabled by default forMongo.startSession().セッションを開始した後は、その
retryWrites設定を変更することはできません。スナップショット
ブール値。 MongoDB 5.0 + 配置のセッションのスナップショット読み取りを有効にします。
causalConsistencyと排他関係にあります。writeConcern
ドキュメント。 書込み保証 ( write concern) を指定します。
セッションを開始した後に設定を変更するには、
Session.getOptions().setWriteConcern()を参照してください。
互換性
このメソッドは、次の環境でホストされている配置で使用できます。
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
注意
This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
例
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());