Docs Menu
Docs Home
/ / /
Java Reactive Streams ドライバー

レプリカセットに対する操作の構成

項目一覧

  • Overview
  • 書込み保証 (write concern)
  • 読み取り保証(read concern)
  • 読み込み設定 (read preference)
  • API ドキュメント

このガイドでは、 書込み保証( write concern ) 、 読み取り保証( read concern ) 、 読み込み設定( read preference ) の構成を使用して、MongoDB がレプリカセットに対して作成、読み取り、アップデート、削除(CRUD)操作を実行する方法を変更する方法を説明します。

これらの構成は、次のレベルで設定できます。

  1. クライアント(オーバーライドされない限り、すべての操作実行にデフォルトを設定します)

  2. トランザクション

  3. Database

  4. コレクション

上記のリストは、優先順位の昇順です。たとえば、クライアント レベルとデータベース レベルの両方で読み取り保証を設定すると、データベース レベルで指定された読み取り保証がクライアント レベルの読み取り保証よりも優先されます。

書込み保証 (write concern) は、書込み (write) 操作が正常に返される前に MongoDB から要求される確認応答のレベルを指定します。明示的な書込み保証(write concern)を指定しない操作は、グローバルなデフォルトの書込み保証(write concern)設定を継承します。

You can set the write concern by using the writeConcern() method on a client or transaction, or by using the withWriteConcern() method on a database or collection.

writeConcern()メソッドとwithWriteConcern()メソッドは、 WriteConcernインスタンスをパラメータとして受け入れます。次のいずれかの値を使用して、書込み保証 (write concern) を指定できます。

  • WriteConcern.ACKNOWLEDGED: The write operation returns after the operation is written to memory.

  • WriteConcern.W1: The write operation returns after only the primary node acknowledges the write operation, without waiting for acknowledgement from secondary nodes.

  • WriteConcern.W2: The write operation returns after the primary node and at least one secondary node acknowledge the write operation.

  • WriteConcern.W3: The write operation returns after the primary node and at least two secondary nodes acknowledge the write operation.

  • WriteConcern.MAJORITY: The write operation returns after a majority of the replica set members acknowledge the write operation.

  • WriteConcern.UNACKNOWLEDGED: The write operation returns after the primary node processes the write operation.

  • WriteConcern.JOURNALED: The write operation returns after the primary node writes the data to the on-disk journal.

次の例では、 MongoClientのインスタンスの書込み保証(write concern)を"majority"に設定します。

MongoClientSettings settings = MongoClientSettings.builder()
.applyConnectionString(new ConnectionString("<your connection string>"))
.writeConcern(WriteConcern.MAJORITY)
.build();
MongoClient client = MongoClients.create(settings);

次の例では、 コレクションの書込み保証(write concern)を"majority"に設定します。

MongoCollection<Document> collection = database.getCollection("<collection name>");
collection = collection.withWriteConcern(WriteConcern.MAJORITY);

注意

コレクションとデータベースは不変

MongoDatabase and MongoCollection instances are immutable. When you set the write concern on a database or collection, the method returns a new instance and does not affect the original instance.

For more information about write concern, see Write Concern in the MongoDB Server manual.

読み取り保証(read concern) は、次の動作を指定します。

読み取り保証 (read concern) は、クライアントまたはトランザクションでreadConcern()メソッドを使用するか、データベースまたはコレクションでwithReadConcern()メソッドを使用して指定できます。 readConcern()メソッドとwithReadConcern()メソッドは、読み取り保証(read concern)レベルを指定する単一のパラメータを受け入れます。

次の読み取り保証レベルを設定できます。

  • ReadConcern.LOCAL: The query returns the instance's most recent data. Provides no guarantee that the data has been written to a majority of the replica set members.

  • ReadConern.AVAILABLE: The query returns the instance's most recent data. Provides no guarantee that the data has been written to a majority of the replica set members. ReadConcern.AVAILABLE is not available for use with causally consistent sessions and transactions.

  • ReadConcern.MAJORITY: The query returns data that has been acknowledged by a majority of the replica set members.

  • ReadConcern.LINEARIZABLE: The query returns data that reflects all successful writes that completed prior to the start of the read operation. ReadConcern.LINEARIZABLE is not available for use with causally consistent sessions and transactions.

  • ReadConcern.SNAPSHOT: The query returns majority-committed data as it appears across shards, from a specific single point in the recent past.

読み取り保証 (read concern) レベルの詳細については、MongoDB サーバー マニュアルの「 読み取り保証(read concern) レベル 」を参照してください。

次の例では、 MongoClientのインスタンスの読み取り保証をReadConcern.MAJORITYに設定します。

MongoClientSettings settings = MongoClientSettings.builder()
.applyConnectionString(new ConnectionString("<your connection string>"))
.readConcern(ReadConcern.MAJORITY)
.build();
MongoClient client = MongoClients.create(settings);

次の例では、 コレクションの読み取り保証をReadConcern.MAJORITYに設定します。

MongoCollection<Document> collection = database.getCollection("<collection name>");
collection = collection.withReadConcern(ReadConcern.MAJORITY);

読み取り保証 (read concern) の詳細については、MongoDB サーバー マニュアルの「読み取り保証(read concern) 」を参照してください。

読み込み設定 (read preference) は、クエリの実行時に MongoDB がレプリカセットのどのノードを読み取るかを決定します。読み込み設定(read preference)は、クライアントまたはトランザクションでreadPreference()メソッドを使用するか、データベースまたはコレクションでwithReadPreference()メソッドを使用して設定できます。

readPreference()メソッドとwithReadPreference()メソッドは、読み込み設定(read preference)モードをパラメータとして受け入れます。読み込み設定 (read preference) モードは、次のいずれかの値に設定できます。

  • ReadPreference.primary(): The query returns data from the primary node.

  • ReadPreference.primaryPreferred(): The query returns data from the primary node if available. Otherwise, the query returns data from a secondary node.

  • ReadPreference.secondary(): The query returns data from a secondary node.

  • ReadPreference.secondaryPreferred(): The query returns data from a secondary node if available, Otherwise, the query returns data from the primary node.

  • ReadPreference.nearest(): The query returns data from the node with the lowest network latency.

次の例では、 MongoClientのインスタンスの読み込み設定(read preference)をReadPreference.secondary()に設定します。

MongoClientSettings settings = MongoClientSettings.builder()
.applyConnectionString(new ConnectionString("<your connection string>"))
.readPreference(ReadPreference.secondary())
.build();

次の例では、 コレクションの読み込み設定(read preference)をReadPreference.secondary()に設定しています。

MongoCollection<Document> collection = database.getCollection("<collection name>");
collection = collection.withReadPreference(ReadPreference.secondary());

読み込み設定(read preference)の詳細については、MongoDB Server マニュアルの読み込み設定(read preference) を参照してください。

このガイドで説明したメソッドや型の詳細については、次の API ドキュメントを参照してください。

戻る

使用中の暗号化