Docs Menu
Docs Home
/ / /
C#/.NET
/

Update Many Documents

On this page

  • Example
  • Expected Result
  • More Information
  • API Documentation

You can update more than one document using the UpdateMany() method on a collection object.

The following code updates all documents in the restaurants collection that have a cuisine field with the value of "Pizza". After the update, these documents will have a cuisine field with a value of "Pasta and breadsticks".

Select the Asynchronous or Synchronous tab to see the corresponding code.

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 filter
var 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);

For a fully runnable example of the UpdateManyAsync() operation, see the UpdateManyAsync code sample.

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 filter
var update = Builders<Restaurant>.Update
.Set(restaurant => restaurant.Cuisine, newValue);
// Updates all documents that have a "cuisine" value of "Pizza"
return _restaurantsCollection.UpdateMany(filter, update);

For a fully runnable example of the UpdateMany() operation, see the UpdateMany code sample.

Running either of the preceding full examples prints the following results:

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

To learn more about updating documents, see the Update Many guide.

To learn more about using builders, see Operations with Builders.

Back

Update a Document