MongoDb c# official driver bulk update

Vladyslav Furdak picture Vladyslav Furdak · Aug 5, 2015 · Viewed 9k times · Source

How can i rewrite the following old code via the new C# MongoDb driver which using IMongoCollection interface :

var bulk = dbCollection.InitializeUnorderedBulkOperation();
foreach (var profile in profiles)
{
   bulk.Find(Query.EQ("_id",profile.ID)).Upsert().Update(Update.Set("isDeleted", true));  
}

bulk.Execute();

How to create Update operation with Builder mechanism is clear for me, but how to perform update bulk operation ?

Answer

rnofenko picture rnofenko · Aug 6, 2015

MongoDB.Driver has UpdateManyAsync

var filter = Builders<Profile>.Filter.In(x => x.Id, profiles.Select(x => x.Id));
var update = Builders<Profile>.Update.Set(x => x.IsDeleted, true);
await collection.UpdateManyAsync(filter, update);