Overview
このガイドでは、 書込み保証( write concern ) 、 読み取り保証( read concern ) 、 読み込み設定( read preference ) の構成を使用して、MongoDB がレプリカセットに対して作成、読み取り、アップデート、削除(CRUD)操作を実行する方法を変更する方法を説明します。
これらの構成は、次のレベルで設定できます。
クライアント(オーバーライドされない限り、すべての操作実行にデフォルトを設定します)
トランザクション
Database
コレクション
上記のリストは、優先順位の昇順です。 例、クライアントレベルとデータベースレベルの両方で読み取り保証を設定すると、データベースレベルで指定された読み取り保証 (read concern)保証がクライアントレベルの読み取り保証 (read concern)よりも優先されます。
書込み保証 (write concern)
書込み保証 (write concern) は、書込み (write)操作が正常に返される前にMongoDBから要求される確認応答のレベルを指定します。 明示的な書込み保証( 書込み保証 (write concern) )を指定しない操作は、グローバルなデフォルトの書込み保証( 書込み保証 (write concern)設定を継承します。
次の関数を呼び出すことで、書込み保証 (write concern)を設定できます。
mongoc_client_set_write_concern()は、クライアントに書込み保証 (write concern)を設定します。mongoc_transaction_opts_set_write_concern()は、トランザクションに書込み保証 (write concern)を設定します。mongoc_database_set_write_concern()は、データベースに書込み保証 (write concern)を設定します。mongoc_collection_set_write_concern()は、 コレクションに書込み保証 (write concern)を設定します。
書込み保証 (write concern)のレベルを指定するには、mongoc_write_concern_set_w() 関数を呼び出し、書込み保証 (write concern)と次のいずれかの値を渡します。
MONGOC_WRITE_CONCERN_W_DEFAULT: 書込み操作は、操作がメモリに書込まれた後に返します。0: 書込み (write)操作は、プライマリノードが書込み (write)操作を処理した後に返します。1: 書込み (write)操作は、セカンダリ ノードからの確認を待つことなく、プライマリノードのみが書込み (write)操作を確認した後に返します。MONGOC_WRITE_CONCERN_W_MAJORITY: 書込み (write)操作は、レプリカセットのノードの過半数が書込み (write)操作を確認した後に返します。MONGOC_WRITE_CONCERN_W_TAG: 書込み (write ) 操作は、指定されたタグを持つレプリカセットノードが書込み (write)操作を確認した後に返します。
次の例では、 mongoc_client_tのインスタンスの書込み保証( 書込み保証 (write concern) )をMONGOC_WRITE_CONCERN_W_MAJORITYに設定します。
// Create a new client instance mongoc_client_t *client = mongoc_client_new("<connection string>"); // Create a new write concern mongoc_write_concern_t *write_concern = mongoc_write_concern_new(); mongoc_write_concern_set_w(write_concern, MONGOC_WRITE_CONCERN_W_MAJORITY); // Set the write concern on the client mongoc_client_set_write_concern(client, write_concern);
次の例では、コレクションの書込み保証 ( 書込み保証 (write concern) ) をMONGOC_WRITE_CONCERN_W_MAJORITYに設定します。
mongoc_collection_t *collection = mongoc_client_get_collection(client, "<database name>", "<collection name>"); // Create a new write concern mongoc_write_concern_t *write_concern = mongoc_write_concern_new(); mongoc_write_concern_set_w(write_concern, MONGOC_WRITE_CONCERN_W_MAJORITY); // Set the write concern on the collection mongoc_collection_set_write_concern(collection, write_concern);
注意
コレクションとデータベースは不変
mongoc_database_t とmongoc_collection_tインスタンスは不変です。 データベースまたはコレクションに書込み保証 ( 書込み保証 (write concern) )を設定すると、 メソッドは新しいインスタンスを返し、元のインスタンスには影響しません。
For more information about write concern, see Write Concern in the MongoDB Server manual.
読み取り保証(read concern)
読み取り保証(read concern) は、次の動作を指定します。
Level of causal consistency across replica sets
クエリ中に維持される分離の保証
読み取り保証 (read concern)は、次の関数を呼び出して指定できます。
mongoc_client_set_read_concern()クライアントに読み取り保証 (read concern)を設定します。mongoc_transaction_opts_set_read_concern()は、トランザクションに読み取り保証 (read concern)を設定します。mongoc_database_set_read_concern()は、 データベースに読み取り保証 (read concern)を設定します。mongoc_collection_set_read_concern()は、 コレクションに読み取り保証 (read concern)を設定します。
読み取り保証 (read concern)のレベルを指定するには、mongoc_read_concern_set_level() 関数を呼び出し、読み取り保証 (read concern)と次のいずれかの値を渡します。
MONGOC_READ_CONCERN_LEVEL_LOCAL: クエリは、インスタンスの最新データを返します。 レプリカセットのノードの過半数にデータが書き込まれたことを保証するものではありません。MONGOC_READ_CONCERN_LEVEL_AVAILABLE: クエリは、インスタンスの最新データを返します。 レプリカセットのノードの過半数にデータが書き込まれたことを保証するものではありません。ReadConcern.AVAILABLEは 因果整合性を持つセッションおよびトランザクションでは 使用できません 。MONGOC_READ_CONCERN_LEVEL_MAJORITY: このクエリは、レプリカセットのノードの過半数が承認したデータを返します。MONGOC_READ_CONCERN_LEVEL_LINEARIZABLE: クエリは、読み取り操作の開始前に完了したすべての成功した書き込みを反映したデータを返します。MONGOC_READ_CONCERN_LEVEL_LINEARIZABLEは 因果整合性を持つセッションおよびトランザクションでは 使用できません 。MONGOC_READ_CONCERN_LEVEL_SNAPSHOT: クエリでは、直近の特定の点のシャード全体に表示れる、過半数のコミット済みデータが返されます。
For more information about the read concern levels, see Read Concern Levels in the MongoDB Server manual.
次の例では、 mongoc_client_tのインスタンスの読み取り保証 (read concern)をMONGOC_READ_CONCERN_LEVEL_MAJORITYに設定します。
mongoc_client_t *client = mongoc_client_new("<connection string>"); // Create a new read concern mongoc_read_concern_t *read_concern = mongoc_read_concern_new(); // Set the read concern level mongoc_read_concern_set_level(read_concern, MONGOC_READ_CONCERN_LEVEL_MAJORITY); // Set the read concern on the client mongoc_client_set_read_concern(client, read_concern);
次の例では、コレクションの読み取り保証 (read concern)をMONGOC_READ_CONCERN_LEVEL_MAJORITYに設定します。
mongoc_collection_t *collection = mongoc_client_get_collection(client, "<database name>", "<collection name>"); // Create a new read concern mongoc_read_concern_t *read_concern = mongoc_read_concern_new(); // Set the read concern level mongoc_read_concern_set_level(read_concern, MONGOC_READ_CONCERN_LEVEL_MAJORITY); // Set the read concern on the collection mongoc_collection_set_read_concern(collection, read_concern);
To learn more about read concern, see Read Concern in the MongoDB Server manual.
読み込み設定 (read preference)
読み込み設定 (read preference) は、クエリの実行中時にMongoDB がレプリカセットのどのノードを読み取るかを決定します。 読み込み設定(読み込み設定 (read preference)は、次の関数を呼び出して設定できます。
mongoc_client_set_read_prefs()は、 クライアントの読み込み設定(読み込み設定 (read preference)を設定します。mongoc_transaction_opts_set_read_prefs()は、トランザクションに読み込み設定 (read preference)を設定します。mongoc_database_set_read_prefs()は、 データベースに読み込み設定 (read preference)を設定します。mongoc_collection_set_read_prefs()は、 コレクションの読み込み設定(読み込み設定 (read preference)を設定します。
読み込み設定(読み込み設定 (read preference)のレベルを指定するには、mongoc_read_prefs_new() 関数を呼び出し、次のいずれかの値を渡します。
MONGOC_READ_PRIMARY: クエリは プライマリノードからのデータを返します。MONGOC_READ_PRIMARY_PREFERRED: クエリは、プライマリノードからのデータが使用可能な場合、返します。 それ以外の場合、クエリは セカンダリノードからのデータを返します。MONGOC_READ_SECONDARY: クエリは セカンダリノードからのデータを返します。MONGOC_READ_SECONDARY_PREFERRED: クエリは セカンダリノードからのデータを返します。それ以外の場合、クエリは プライマリノードからのデータを返します。MONGOC_READ_NEAREST: クエリは、ネットワークレイテンシが最も低いノードからのデータを返します。
次の例では、 mongoc_client_tのインスタンスの読み込み設定( 読み込み設定 (read preference) )をMONGOC_READ_SECONDARYに設定します。
mongoc_client_t *client = mongoc_client_new("<connection string>"); // Create a new read preference mongoc_read_prefs_t *read_prefs = mongoc_read_prefs_new(MONGOC_READ_SECONDARY); // Set the read preference on the client mongoc_client_set_read_prefs(client, read_prefs);
次の例では、コレクションの読み込み設定( 読み込み設定 (read preference) )をMONGOC_READ_SECONDARYに設定しています。
mongoc_collection_t *collection = mongoc_client_get_collection(client, "<database name>", "<collection name>"); // Create a new read preference mongoc_read_prefs_t *read_prefs = mongoc_read_prefs_new(MONGOC_READ_SECONDARY); // Set the read preference on the collection mongoc_collection_set_read_prefs(collection, read_prefs);
読み込み設定 ( 読み込み設定 (read preference)) の詳細については、 MongoDB Serverマニュアルの読み込み設定(read preference) を参照してください。
照合
コレクションに対して読み取り操作および書き込み (write) 操作を実行するときに、照合を指定できます。
照合は、大文字と小文字やアクセント記号など、string を比較するための言語固有のルールのセットです。
照合を指定するには、読み取り操作または書き込み操作のオプションで照合定義を "collation" キーに渡します。
照合の例
以下のドキュメントを持つコレクションを考えてみましょう。
{ _id: 1, category: "café" }, { _id: 2, category: "cafe" }, { _id: 3, category: "cafE" }
次の例では、ロケールのロケールを指定し、大文字と小文字と文字の違いを無視する照合を作成しています。次に、この照合を使用して、categoryフィールド値が "cafe" と一致するドキュメントを検索します。指定された照合により、クエリは 3 つのドキュメントすべてを返します。
mongoc_collection_t *collection = mongoc_client_get_collection(client, "<database name>", "<collection name>"); bson_t *opts = BCON_NEW( "collation", "{", "locale", BCON_UTF8("fr"), "strength", BCON_INT32(1), "}" ); mongoc_cursor_t *cursor = mongoc_collection_find_with_opts( collection, BCON_NEW("category", BCON_UTF8("cafe")), opts, NULL ); const bson_t *doc; while (mongoc_cursor_next(cursor, &doc)) { char *str = bson_as_canonical_extended_json(doc, NULL); printf("%s\n", str); bson_free(str); }
照合の詳細については、 MongoDB Serverマニュアルの「 照合 」参照を参照してください。
API ドキュメント
このガイドで説明した関数や型の詳細については、次のAPIドキュメントを参照してください。