Operators
Set operator - $set
The $set operator is used to update the value of a specified field.
Builders<T<.Update.Set(doc => doc.<field>, <value>)The sample updates the FirstName field of the first document in the collection.
var collection = database
.GetCollection<User>(Constants.UsersCollection);
// create an empty filter
var firstUserFilter = Builders<User>.Filter.Empty;
// create a Set operator update definition
var updateNameDefinition = Builders<User>.Update
.Set(u => u.FirstName, "Chris");
// update the document
var updateNameResult = await collection
.UpdateOneAsync(firstUserFilter,
updateNameDefinition);var bsonCollection = database
.GetCollection<BsonDocument>(Constants.UsersCollection);
var bsonUpdateNameDefinition = Builders<BsonDocument>
.Update.Set("firstName", "John");
var bsonUpdateNameResult = await bsonCollection
.UpdateOneAsync(bsonFirstUserFilter,
bsonUpdateNameDefinition);Multiple fields update
You can update multiple document's fields in one operation by declaring more than one update definitions.
The sample updates the first document's Phone, Website and FavoriteSports (array field).
Inc operator - $inc
The $inc operator is used to increase the value of a specified field by a specified amount.
The sample increments the first document's salary.
The $inc operator can be used with negative values to decrease a specified field's value
Min operator - $min
The $min operator is used to update the value of a specified field only if the new value is less than the current value.
The sample decreases the first document's salary value from 3000 to 2000 π° .
Max operator - $max
The $max operator is used to update the value of a specified field only if the new value is greater than the current value.
The sample increases the first document's salary value from 3000 to 3500 π° .
Mul operator - $mul
The $mul operator is used to multiply the current value of a specified field by a specified value.
The sample doubles the first document's salary value from 1000 to 2000 using the $mul operator π° .
Unset operator - $unset
The $unset operator is used to remove a field from a document.
The sample removes β the Website field from a user document.
ββ β‘ Danger β β‘ββ Danger β β‘ββ Danger β β‘
Use caution when applying the Unset operator!! In case you unset a non nullable property then you might end up having unexpected results. For example, if you unset the salary field on a user document, which is a decimal field, then the next time you read that document, the salary will be 0, not null!
Rename operator - $rename
The $rename operator is used to rename a field.
The sample updates the Phone field of the first document to PhoneNumber using the $rename operator.
When renaming document fields make sure the new field names can be matched back you your C# models. You can control the field's name in the database using the[BsonElement] attribute. In the following example, the Phone property will be saved as phoneNumber in the database while can be deserialized back to the Phone property properly.
Last updated