πŸ“Update

Overview

MongoDB provides a set of update operators to help you update values on your collections' documents. This section contains several samples showing how to use these operators to update:

  • Top level fields

  • Embedded fields

  • Array fields

The section contains samples for the following operators:

Operator

Description

Set

Sets the value of a field in a document

Inc

Increments a field's value

Min

Updates a field's value only if the specified value is less than the existing

Max

Updates a field's value only if the specified value is greater than the existing

Mul

Multiplies a field's value by a specified amount

Unset

Removes the specified field from the document

Rename

Renames a field

Other than the update operations there are also samples showing how to replace documents and array fields.

Update Definition Builder πŸ’ͺ

To create update operations using the MongoDB C# driver you need to create one or more update definitions. The syntax to create an update definition is the following:

Syntax: Builders<T>.Update.<Operator>(<field>,<value>)

UpdateOne & UpdateMany

To update one or more documents use the UpdateOne and UpdateMany IMongoCollection<T> respectively.

Syntax: IMongoCollection<T>.UpdateOne(<filter>,<update-definition>)

Syntax: IMongoCollection<T>.UpdateMany(<filter>,<update-definition>)

Last updated