Search⌘ K

Update, Delete & Index Operations

Explore how to use MongoDB's update, delete, and index operations within C# and .NET Core. Learn to update multiple documents, delete users, and create indexes dynamically or with strong typing. Understand how MongoDB's flexible schema supports dynamic data storage and mapping JSON documents to .NET objects for easier management.

In the previous lesson, we looked at how the basic CRUD operations, create and read, could be performed using C#. Let’s learn about some more of them below.

Update Users

Documents can be updated in a similar manner:

C#
public async Task<bool> UpdateUser(ObjectId id, string udateFieldName, string updateFieldValue)
{
var filter = Builders<User>.Filter.Eq("_id", id);
var update = Builders<User>.Update.Set(udateFieldName, updateFieldValue);
var result = await _usersCollection.UpdateOneAsync(filter, update);
return result.ModifiedCount != 0;
}

In this example, function UpdateOneAsync is used, but if you want to change values of multiple documents, you can use UpdateMultipleAsync.

Synchronous siblings of these operations also ...