Search code examples
c#mongodbmongodb-.net-drivermongodb-csharp-2.0

MongoDb c# official driver bulk update


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 ?


Solution

  • 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);