I am using MongoDB 2, and I want to update multiple documents and upsert a value like processed:true
into the collection. But MongoDB c# api only allows us to
For those using version 2.0 of the MongoDB.Driver, you can make use of the BulkWriteAsync method.
// our example list
List products = GetProductsFromSomewhere();
var collection = YourDatabase.GetCollection("products");
// initialise write model to hold list of our upsert tasks
var models = new WriteModel[products.Count];
// use ReplaceOneModel with property IsUpsert set to true to upsert whole documents
for (var i = 0; i < products.Count; i++){
var bsonDoc = products[i].ToBsonDocument();
models[i] = new ReplaceOneModel(new BsonDocument("aw_product_id", products[i].aw_product_id), bsonDoc) { IsUpsert = true };
};
await collection.BulkWriteAsync(models);