コレクション オブジェクトの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 コードサンプル を参照してください。
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 _restaurantsCollection.UpdateMany(filter, update);UpdateMany()操作の完全に実行可能な例については、UpdateMany コードサンプル を参照してください。
期待される結果
前述の例のいずれかを実行すると、次の結果が出力されます。
Restaurants with cuisine "Pizza" found: 1163Restaurants modified by update: 1163Restaurants with cuisine "Pasta and breadsticks" found after update: 1163Resetting sample data...done.詳細情報
ドキュメントの更新について詳しくは、「 多数更新ガイド 」を参照してください。
ビルダの使用の詳細については、「 ビルダを使用した操作 」を参照してください。