Update Fields
On this page
Overview
On this page, you can learn how to use the MongoDB .NET/C# Driver to update
fields in multiple MongoDB documents. This page describes how to create UpdateDefinition<TDocument>
objects that specify the update operations you want to perform on fields.
You can pass these objects to the update methods described on the Update Many
page.
The .NET/C# Driver supports the field update operators described in the
MongoDB Server manual. To specify an
update operation, call the corresponding method from the Builders.Update
property.
The following sections describe these methods in more detail.
Note
Method Overloads
Many of the methods on this page have multiple overloads. The examples in this guide show only one definition of each method. For more information about the available overloads, see the API documentation.
Sample Data
The examples in this guide use the restaurants
collection
from the sample_restaurants
database. The documents in this
collection 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 snake-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.
This collection is from the sample datasets provided by Atlas. See the Quick Start to learn how to create a free MongoDB cluster and load this sample data.
Increment a Value
To increment the value of a field by a specific amount, call the Builders.Update.Inc()
method. This method accepts the following parameters:
Parameter | Description |
---|---|
| An expression that specifies the field to increment. Data Type: |
| The amount to increment the field by. Data Type: |
Multiply a Value
To multiply the value of a field by a specific amount, call the Builders.Update.Mul()
method. This method accepts the following parameters:
Parameter | Description |
---|---|
| An expression that specifies the field to update. Data Type: |
| The amount to multiply the field by. Data Type: |
Rename a Field
To rename a field, call the Builders.Update.Rename()
method. This method accepts
the following parameters:
Parameter | Description |
---|---|
| An expression that specifies the field to rename. Data Type: |
| The new name for the field. Data Type: |
Set a Value
To set the value of a field to a specific value, call the Builders.Update.Set()
method. This method accepts the following parameters:
Parameter | Description |
---|---|
| An expression that specifies the field to update. Data Type: |
| The value to set the field to. Data Type: |
Set by Comparison
To update the value of the field to a specified value, but only if the specified value
is greater than the current value of the field, call the Builders.Update.Max()
method. This method accepts the following parameters:
Parameter | Description |
---|---|
| An expression that specifies the field to update. Data Type: |
| The value to set the field to. Data Type: |
To update the value of the field to a specified value, but only if the specified value
is less than the current value of the field, call the Builders.Update.Min()
method. This method accepts the following parameters:
Parameter | Description |
---|---|
| An expression that specifies the field to update. Data Type: |
| The value to set the field to. Data Type: |
Set on Insert
To set the value of a field only if the document was upserted by the same operation, call the
Builders.Update.SetOnInsert()
method. This method accepts the following parameters:
Parameter | Description |
---|---|
| An expression that specifies the field to update. Data Type: |
| The value to set the field to. Data Type: |
Set the Current Date
To set the value of a field to the current date and time, call the
Builders.Update.CurrentDate()
method. This method accepts the following parameters:
Parameter | Description |
---|---|
| An expression that specifies the field to update. Data Type: |
| The format of the date and time, defined in the Data Type: UpdateDefinitionCurrentDateType? |
Unset a Field
To remove a field from a document, call the Builders.Update.Unset()
method. This
method accepts the following parameter:
Parameter | Description |
---|---|
| An expression that specifies the field to remove. Data Type: |
API Documentation
For more information about any of the methods discussed in this guide, see the following API documentation: