I have multiple MongoDB documents like this:
{
"_id":"abcde",
"Students":[
{"Name":"John","IsNew":true},
{"Name":"Steve","IsNew":true}
],
}
{
"_id":"fghij",
"Students":[
{"Name":"Ron","IsNew":true},
{"Name":"Mike","IsNew":true}
],
}
How to update the IsNew
field to false for all students for every document using C# driver?
You can use UpdateMany
method from MongoDB C# driver with the positional all operator:
var filter = Builders<YourModel>.Filter.Exists(x => x.Students);
FieldDefinition<YourModel, bool> field = "Students.$[].IsNew";
var update = Builders<YourModel>.Update.Set(field, false);
Col.UpdateMany(filter, update);
EDIT: you can use .Exists()
as a filter to make sure that Students
array is present in all the documents that are being updated