AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。

多くのドキュメントの更新

コレクション オブジェクトのUpdateMany()メソッドを使用して、複数のドキュメントを更新できます。

次のコードは、 cuisineフィールドの値が「Pizza」である、 restaurantsコレクション内のすべてのドキュメントを更新します。 更新後、これらのドキュメントには「Pasta and cake」の値を持つcuisineフィールドが含まれます。

AsynchronousSynchronous対応するコードを表示するには、 タブまたは タブを選択します。

const string oldValue = "Pizza";const string newValue = "Pasta and breadsticks"; // Creates a filter for all documents with a "cuisine" value of "Pizza"var filter = Builders<Restaurant>.Filter .Eq(restaurant => restaurant.Cuisine, oldValue); // Creates instructions to update the "cuisine" field of documents that// match the filtervar update = Builders<Restaurant>.Update .Set(restaurant => restaurant.Cuisine, newValue); // Updates all documents that have a "cuisine" value of "Pizza"return await _restaurantsCollection.UpdateManyAsync(filter, update);

UpdateManyAsync()操作の完全に実行可能な例については、UpdateManyAsync コードサンプル を参照してください。

前述の例のいずれかを実行すると、次の結果が出力されます。

Restaurants with cuisine "Pizza" found: 1163Restaurants modified by update: 1163Restaurants with cuisine "Pasta and breadsticks" found after update: 1163Resetting sample data...done.

ドキュメントの更新について詳しくは、「 多数更新ガイド 」を参照してください。

ビルダの使用の詳細については、「 ビルダを使用した操作 」を参照してください。