Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDb c# official driver bulk update

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 ?

like image 562
Vladyslav Furdak Avatar asked Aug 05 '15 14:08

Vladyslav Furdak


People also ask

Can you use MongoDB with C#?

By developing with C# and MongoDB together one opens up a world of possibilities. Console, window, and web applications are all possible. As are cross-platform mobile applications using the Xamarin framework.

Can I use MongoDB with C++?

Welcome to the documentation site for the official MongoDB C++ driver. You can add the driver to your application to work with MongoDB using the C++11 or later standard.

What is a MongoDB driver?

The official MongoDB Node. js driver allows Node. js applications to connect to MongoDB and work with data. The driver features an asynchronous API which allows you to interact with MongoDB using Promises or via traditional callbacks.


1 Answers

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);
like image 189
rnofenko Avatar answered Sep 27 '22 01:09

rnofenko