9

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对我来说很清楚,但是如何执行更新批量操作?

4

1 回答 1

13

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);
于 2015-08-06T01:10:11.157 回答