Update Many Documents
You can update more than one document using the UpdateMany()
method on
a collection object.
Example
The examples on this page use the following Restaurant
, Address
, and GradeEntry
classes as models:
public class Restaurant { public ObjectId Id { get; set; } public string Name { get; set; } [ ] public string RestaurantId { get; set; } public string Cuisine { get; set; } public Address Address { get; set; } public string Borough { get; set; } public List<GradeEntry> Grades { get; set; } }
public class Address { public string Building { get; set; } [ ] public double[] Coordinates { get; set; } public string Street { get; set; } [ ] public string ZipCode { get; set; } }
public class GradeEntry { public DateTime Date { get; set; } public string Grade { get; set; } public float Score { get; set; } }
Note
The documents in the restaurants
collection use the camel-case naming
convention. The examples in this guide use a ConventionPack
to deserialize the fields in the collection into Pascal case and map them to
the properties in the Restaurant
class.
To learn more about custom serialization, see Custom Serialization.
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.
Expected Result
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.
More Information
To learn more about updating documents, see the Change Documents guide.
To learn more about using builders, see Operations with Builders.