MongoDb c# official driver bulk update
Asked Answered
P

2

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 ?

Payment answered 5/8, 2015 at 14:22 Comment(0)
T
13

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);
Twana answered 6/8, 2015 at 1:10 Comment(1)
I'm glad this answer cover op necesities, but it doesn't answer the original question. Solution here: #35687970Gereld
I
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);
Islean answered 21/3, 2022 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.