如何通过使用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
操作对我来说很明确,但如何执行更新批量操作?
答案 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);