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

Update Fields

On this page

  • Overview
  • Sample Data
  • Increment a Value
  • Multiply a Value
  • Rename a Field
  • Set a Value
  • Set by Comparison
  • Set on Insert
  • Set the Current Date
  • Unset a Field
  • API Documentation

On this page, you can learn how to use the MongoDB .NET/C# Driver to update fields in one MongoDB document. 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 One 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.

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; }
[BsonElement("restaurant_id")]
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; }
[BsonElement("coord")]
public double[] Coordinates { get; set; }
public string Street { get; set; }
[BsonElement("zipcode")]
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.

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

field

An expression that specifies the field to increment.

Data Type: Expression<Func<TDocument, TField>>

value

The amount to increment the field by.

Data Type: TField

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

field

An expression that specifies the field to update.

Data Type: Expression<Func<TDocument, TField>>

value

The amount to multiply the field by.

Data Type: TField

To rename a field, call the Builders.Update.Rename() method. This method accepts the following parameters:

Parameter
Description

field

An expression that specifies the field to rename.

Data Type: Expression<Func<TDocument, TField>>

newName

The new name for the field.

Data Type: string

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

field

An expression that specifies the field to update.

Data Type: Expression<Func<TDocument, TField>>

value

The value to set the field to.

Data Type: TField

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

field

An expression that specifies the field to update.

Data Type: Expression<Func<TDocument, TField>>

value

The value to set the field to.

Data Type: TField

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

field

An expression that specifies the field to update.

Data Type: Expression<Func<TDocument, TField>>

value

The value to set the field to.

Data Type: TField

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

field

An expression that specifies the field to update.

Data Type: Expression<Func<TDocument, TField>>

value

The value to set the field to.

Data Type: TField

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

field

An expression that specifies the field to update.

Data Type: Expression<Func<TDocument, TField>>

type

The format of the date and time, defined in the UpdateDefinitionCurrentDateType enum. The default value is null.

Data Type: UpdateDefinitionCurrentDateType?

To remove a field from a document, call the Builders.Update.Unset() method. This method accepts the following parameter:

Parameter
Description

field

An expression that specifies the field to remove.

Data Type: Expression<Func<TDocument, TField>>

For more information about any of the methods discussed in this guide, see the following API documentation:

Back

Update One