Overview
このガイドでは、 C++ドライバーを使用してMongoDBコレクション内のドキュメントを更新する方法を学習できます。 単一のドキュメントを更新するにはupdate_one()メソッドを、複数のドキュメントを更新するにはupdate_many()メソッドを呼び出します。
サンプル データ
The examples in this guide use the restaurants collection in the sample_restaurants database from the Atlas sample datasets. To access this collection from your C++ application, instantiate a mongocxx::client that connects to an Atlas cluster and assign the following values to your db and collection variables:
auto db = client["sample_restaurants"]; auto collection = db["restaurants"];
MongoDB Atlasクラスターを無料で作成して、サンプルデータセットをロードする方法については、MongoDBを使い始めるガイドを参照してください。
アップデート操作
MongoDB では、次の方法で更新操作を実行できます。
update_one()は、検索条件に一致する最初のドキュメントを更新します。update_many()は、検索条件に一致するすべてのドキュメントを更新します
各更新方法には次のパラメーターが必要です。
クエリフィルタードキュメント: 更新するドキュメントを指定します。 クエリフィルターの詳細については、 MongoDB Serverマニュアルの「クエリフィルター ドキュメント 」セクションを参照してください。
ドキュメントの更新:更新演算子、または実行する更新の種類と、変更するフィールドと値を指定します。 更新演算子とその使用方法のリストについては、 MongoDB Serverマニュアルの「フィールド更新演算子 」のガイドを参照してください。
1 つのドキュメントの更新
次の例では、 update_one()メソッドを使用して、 restaurantsコレクション内のドキュメントのname値を"Bagels N Buns"から"2 Bagels 2 Buns"に更新します。
auto query_filter = make_document(kvp("name", "Bagels N Buns")); auto update_doc = make_document(kvp("$set", make_document(kvp("name", "2 Bagels 2 Buns")))); auto result = collection.update_one(query_filter.view(), update_doc.view());
多くのドキュメントの更新
次の例では、 update_many()メソッドを使用して、 cuisineの値が"Pizza"であるすべてのドキュメントを更新します。 更新後、ドキュメントのcuisine値は"Pasta"になります。
auto query_filter = make_document(kvp("cuisine", "Pizza")); auto update_doc = make_document(kvp("$set", make_document(kvp("cuisine", "Pasta")))); auto result = collection.update_many(query_filter.view(), update_doc.view());
アップサート オプション
upsert パラメーターを使用すると、update_one() 操作と update_many() 操作を使用して 条件付き操作を実行できます。
指定されたドキュメントが存在する場合、 コマンドはそれを更新します。
指定されたドキュメントが存在しない場合、コマンドは指定されたパラメーターを持つ新しいドキュメントを作成します。
詳細については、「更新の例の変更」セクションまたはこのページのオプション テーブルを参照してください。
更新操作をカスタマイズする
mongocxx::options::updateクラスのインスタンスを任意のパラメータとして渡すことで、 update_one()メソッドとupdate_many()メソッドの動作を変更できます。 次の表では、 mongocxx::options::updateインスタンスに設定できるフィールドを説明しています。
フィールド | 説明 |
|---|---|
| Specifies whether the update operation performs an upsert operation if no documents match the query filter. For more information, see the upsert statement in the MongoDB Server manual. |
| Specifies whether the update operation bypasses document validation. This lets you update documents that don't meet the schema validation requirements, if any exist. For more information about schema validation, see Schema Validation in the MongoDB Server manual. |
| Specifies the kind of language collation to use when sorting results. For more information, see Collation in the MongoDB Server manual. |
| 操作で配列フィールドが変更される場合に、アップデートを適用する配列要素を指定します。 |
| Sets the index to scan for documents. For more information, see the hint statement in the MongoDB Server manual. |
| Sets the write concern for the operation. For more information, see Write Concern in the MongoDB Server manual. |
| Specifies a document with a list of values to improve operation readability. Values must be constant or closed expressions that don't reference document fields. For more information, see the let statement in the MongoDB Server manual. |
|
変更更新の例
次の例では、update_many() メソッドを使用して、borough の値が "Manhattan" であるすべてのドキュメントを検索します。次に、これらのドキュメントの borough の値を "Manhattan (north)" に更新します。
upsert オプションが true に設定されているため、クエリフィルターが既存のドキュメントと一致しない場合、 C++ドライバーは フィルター と 更新 ドキュメントのフィールドと値を含む新しいドキュメントを挿入します。
mongocxx::options::update opts{}; opts.upsert(true); auto query_filter = make_document(kvp("borough", "Manhattan")); auto update_doc = make_document(kvp("$set", make_document(kvp("borough", "Manhattan (north)")))); auto result = collection.update_many(query_filter.view(), update_doc.view(), opts);
戻り値
update_one()メソッドとupdate_many()メソッドはmongocxx::result::updateクラスのインスタンスを返します。 このクラスには、次のメンバー関数が含まれています。
関数 | 説明 |
|---|---|
| アップデートされた数に関係なく、クエリフィルターに一致したドキュメントの数を返します。 |
| 更新操作によって変更されたドキュメントの数を返します。 更新されたドキュメントが元と同一の場合、このカウントには含まれません。 |
| 操作の未加工の結果ドキュメントを返します。 |
| データベースにアップサートされたドキュメントの数を返します。 |
| ドライバーがアップサートを実行した場合、データベースでアップサートされたドキュメントのIDを返します。 |
次の例では、 update_many()メソッドを使用して、一致するドキュメントのnameフィールドを"Dunkin' Donuts"から"Dunkin'"にアップデートします。 変更されたドキュメントの数を出力するには、 modified_count()メンバー関数を呼び出します。
auto query_filter = make_document(kvp("name", "Dunkin' Donuts")); auto update_doc = make_document(kvp("$set", make_document(kvp("name", "Dunkin'")))); auto result = collection.update_many(query_filter.view(), update_doc.view()); std::cout << "Modified documents: " << result->modified_count() << std::endl;
Modified documents: 206
詳細情報
クエリフィルターの作成の詳細については、「クエリの指定」ガイドを参照してください。
API ドキュメント
このガイドで説明したメソッドや型の詳細については、次の API ドキュメントを参照してください。