10

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 ?

Vladyslav Furdak
  • 1,765
  • 2
  • 22
  • 46

2 Answers2

14

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);
rnofenko
  • 9,198
  • 2
  • 46
  • 56
  • 6
    I'm glad this answer cover op necesities, but it doesn't answer the original question. Solution here: http://stackoverflow.com/questions/35687470/c-sharp-mongodb-driver-2-0-how-to-upsert-in-a-bulk-operation/35688613#35688613 – Adrian Lopez Feb 28 '16 at 22:00
0

on new Version MongoDB.Driver can be set Flag

var query = Query<Profile>.In(p => p.ID, profiles.Select(x => x.Id));
var update = Update<Profile>.Set(p => p.IsDeleted, true);
Collection.Update(query, update, UpdateFlags.Multi);