C # mongodb driver 2.0 - How to activate bulk operation?

I switched from 1.9 to 2.2 and read the documentation . I was surprised to find that it is no longer possible to resume during a mass operation because operations do not allow options.

bulkOps.Add(new UpdateOneModel<BsonDocument>(filter, update)); collection.BulkWrite(bulkOps); 

Must be

 options.isUpsert = true; bulkOps.Add(new UpdateOneModel<BsonDocument>(filter, update, options)); collection.BulkWrite(bulkOps); 

Is this work incomplete, intended, or am I missing something? Thanks.

+4
c # mongodb batch-processing mongodb-.net-driver bulk
source share
2 answers

Set the IsUpsert property to UpdateOneModel true to enable the update in upsert.

 var upsertOne = new UpdateOneModel<BsonDocument>(filter, update) { IsUpsert = true }; bulkOps.Add(upsertOne); collection.BulkWrite(bulkOps); 
+11
source share

this collection of mangoes

 IMongoCollection<T> collection 

and list the entries to insert, where T has an Id field.

 IEnumerable<T> records 

this fragment will perform mass upsert (the filter condition may be changed depending on the situation):

 var bulkOps = new List<WriteModel<T>>(); foreach (var record in records) { var upsertOne = new ReplaceOneModel<T>( Builders<T>.Filter.Where(x => x.Id == record.Id), record) { IsUpsert = true }; bulkOps.Add(upsertOne); } collection.BulkWrite(bulkOps); 
+1
source share

All Articles