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.

Update/BasicOperators.cs
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);

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).

Update/BasicOperators.cs
var collection = database
        .GetCollection<User>(Constants.UsersCollection);

var multiUpdateDefinition = Builders<User>.Update
        .Set(u => u.Phone, "123-456-789")
        .Set(u => u.Website, "https://chsakell.com")
        .Set(u => u.FavoriteSports, 
                new List<string> { "Soccer", "Basketball" });

var multiUpdateResult = await collection
        .UpdateOneAsync(firstUserFilter, 
                multiUpdateDefinition);

Inc operator - $inc

The $inc operator is used to increase the value of a specified field by a specified amount.

Builders<T<.Update.Inc(doc => doc.<field>, <value>)

The sample increments the first document's salary.

Update/BasicOperators.cs
var collection = database
            .GetCollection<User>(Constants.UsersCollection);

// create an empty filter
var firstUserFilter = Builders<User>.Filter.Empty;

// create an $inc update definition
var incrementSalaryDefinition = Builders<User>
            .Update.Inc(u => u.Salary, 450);
            
var incrementSalaryResult = await collection
            .UpdateOneAsync(firstUserFilter, 
                        incrementSalaryDefinition);

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.

Builders<T<.Update
    .Min(doc => doc.<field>, <value>)

The sample decreases the first document's salary value from 3000 to 2000 πŸ’° .

Update/BasicOperators.cs
var collection = database
    .GetCollection<User>(Constants.UsersCollection);

// create an empty filter
var firstUserFilter = Builders<User>.Filter.Empty;

// preparation - set current salary to 3000
// for demo only
await collection.UpdateOneAsync(firstUserFilter, Builders<User>
    .Update.Set(u => u.Salary, 3000));
    
// update only if the new value is less than the current
var minUpdateDefinition = Builders<User>
    .Update.Min(u => u.Salary, 2000);
    
// 2000 is less than 3000 so update succeeds
var minUpdateResult = await collection
    .UpdateOneAsync(firstUserFilter, minUpdateDefinition);

Of course if the new value is equal to the current one, the update result will return that no documents updated.

{
	"acknowledged" : true,
	"matchedCount" : 1,
	"modifiedCount" : 0
}

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.

Builders<T<.Update
    .Max(doc => doc.<field>, <value>)

The sample increases the first document's salary value from 3000 to 3500 πŸ’° .

Update/BasicOperators.cs
var collection = database
    .GetCollection<User>(Constants.UsersCollection);

// create an empty filter
var firstUserFilter = Builders<User>.Filter.Empty;

// preparation - set current salary to 3000
// for demo only
await collection.UpdateOneAsync(firstUserFilter, Builders<User>
    .Update.Set(u => u.Salary, 3000));
    
// update only if the new value is greater than the current
var maxUpdateDefinition = Builders<User>
    .Update.Max(u => u.Salary, 3500);
    
// 3500 is greater than 3000 so update succeeds
var maxUpdateResult = await collection
    .UpdateOneAsync(firstUserFilter, maxUpdateDefinition);

Mul operator - $mul

The $mul operator is used to multiply the current value of a specified field by a specified value.

Builders<T<.Update.Mul(doc => doc.<field>, <value>)

The sample doubles the first document's salary value from 1000 to 2000 using the $mul operator πŸ’° .

Update/BasicOperators.cs
var collection = database
    .GetCollection<User>(Constants.UsersCollection);

// create an empty filter
var firstUserFilter = Builders<User>.Filter.Empty;

// preparation - set current salary to 1000
// for demo only
await collection.UpdateOneAsync(firstUserFilter, 
    Builders<User>.Update.Set(u => u.Salary, 1000));
    
// multiply the current salary value by 2
var mulUpdateDefinition = Builders<User>
    .Update.Mul(u => u.Salary, 2);
    
var mulUpdateResult = await collection
    .UpdateOneAsync(firstUserFilter, mulUpdateDefinition);

Unset operator - $unset

The $unset operator is used to remove a field from a document.

Builders<T<.Update.Unset(doc => doc.<field>)

The sample removes ❌ the Website field from a user document.

Update/BasicOperators.cs
var collection = database
    .GetCollection<User>(Constants.UsersCollection);

// create an empty filter
var firstUserFilter = Builders<User>.Filter.Empty;

// remove the website field
var removeWebsiteDefinition = Builders<User>
    .Update.Unset(u => u.Website);

var removeWebsiteFieldUpdateResult = await collection
    .UpdateOneAsync(firstUserFilter, 
        removeWebsiteDefinition);

​​ ⚑ Danger ​ βš‘β€‹β€‹ Danger ​ βš‘β€‹β€‹ Danger ​ ⚑

Rename operator - $rename

The $rename operator is used to rename a field.

Builders<T<.Update
    .Rename(doc => doc.<field>, <new-name>)

The sample updates the Phone field of the first document to PhoneNumber using the $rename operator.

Update/BasicOperators.cs
var collection = database
    .GetCollection<User>(Constants.UsersCollection);

// create an empty filter
var firstUserFilter = Builders<User>.Filter.Empty;

// rename the phone field to phoneNumber for the first user
var renamePhoneDefinition = Builders<User>.
    Update.Rename(u => u.Phone, "phoneNumber");

// update the document
var renamePhoneFieldUpdateResult = await collection
    .UpdateOneAsync(firstUserFilter, 
        renamePhoneDefinition);

Last updated