Mongo.startSession()
定義
Mongo.startSession(<options>)
Starts a session for the connection.
mongosh
assigns the session ID to commands associated with the session.重要
mongosh メソッド
このページでは、
mongosh
メソッドについて記載しています。ただし、データベースコマンドや Node.js などの言語固有のドライバーのドキュメントには該当しません。データベースコマンドについては、
startSession
コマンドを参照してください。MongoDB API ドライバーについては、各言語の MongoDB ドライバー ドキュメントを参照してください。
セッションは、セッションを作成した
MongoClient
オブジェクトでのみ使用できます。 1 つのセッションを同時に使用することはできません。 1 つのセッションを使用する操作は連続して実行する必要があります。The
startSession()
method can take a document with session options. The options available are:フィールド説明causalConsistency
ブール値。 セッションの因果整合性を有効または無効にします。
Mongo.startSession()
enablescausalConsistency
by default. Mutually exclusive withsnapshot
.After starting a session, you cannot modify its
causalConsistency
setting.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, seeMongo.setCausalConsistency()
.readConcern
ドキュメント。 読み取り保証 ( read concern) を指定します。
To modify the setting after starting a session, see
Session.getOptions().setReadConcern()
.readPreference
ドキュメント。 読み込み設定 ( read preference) を指定します。
The readPreference document contains the
mode
field and the optionaltags
field:{ 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
mongosh
with the--retryWrites
option,retryWrites
is enabled by default forMongo.startSession()
.After starting a session, you cannot modify its
retryWrites
setting.スナップショット
Boolean. Enables snapshot reads for the session for MongoDB 5.0+ deployments. Mutually exclusive with
causalConsistency
.writeConcern
ドキュメント。 書込み保証 ( write concern) を指定します。
To modify the setting after starting a session, see
Session.getOptions().setWriteConcern()
.
互換性
このメソッドは、次の環境でホストされている配置で使用できます。
MongoDB Atlas はクラウドでの MongoDB 配置のためのフルマネージド サービスです
注意
このコマンドは、すべての MongoDB Atlas クラスターでサポートされています。すべてのコマンドに対する Atlas のサポートについては、「サポートされていないコマンド」を参照してください。
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());