MongoDb c#官方驱动批量更新

时间:2015-08-05 14:22:19

标签: c# mongodb mongodb-.net-driver mongodb-csharp-2.0

如何通过使用IMongoCollection接口的新C#MongoDb驱动程序重写以下旧代码:

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

bulk.Execute();

如何使用Update机制创建Builder操作对我来说很明确,但如何执行更新批量操作?

1 个答案:

答案 0 :(得分:9)

MongoDB.Driver有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);