1

I have a source file of records ~100000, some of these records are in the db and some are new. What is the fastest way to check each document if it exists, update it if it does, or insert it if its new using MongoDb C# driver.

I have used FindOneAndUpdateAsync on each document but it is taking a long time. I cannot find a way of running multiple of these using the MongoDb driver. I would have to run these parallel in code async?

Carl Thomas
  • 3,605
  • 6
  • 38
  • 50
  • Why don't you check if the performance improves if you start a new `Task` for each `FindOneAndUpdateAsync`? – hyankov Nov 22 '16 at 13:43
  • Use bulk update, [check this](http://stackoverflow.com/questions/7934768/how-to-update-and-upsert-mulitple-documents-in-mongodb-using-c-sharp-drivers) it will help you :) – Neo-coder Nov 22 '16 at 13:45

3 Answers3

0

For the update/insert issue, ReplaceOne takes UpdateOptions as parameter, here you can specify if it should be an upsert. Upsert inserts if not existing, updates if exists. Example code (There's one async as well):

collection.ReplaceOne(query, model, new UpdateOptions() {IsUpsert = upsert});

Alt:

var options = new UpdateOptions { IsUpsert = true };
var result = await collection.UpdateManyAsync(filter, update, options);
SJFJ
  • 657
  • 6
  • 18
0

You can use upsert for your need like below (an example quoted from MongoDB documentation)

var bulk = db.items.initializeUnorderedBulkOp();
bulk.find( { item: "abc123" } ).upsert().replaceOne(
   {
     item: "abc123",
     status: "P",
     points: 100,
   }
);
bulk.execute();

On your C# driver you can use UpdateManyAsync() OR ReplaceOneAsync() with IsUpsert update option set to true

Rahul
  • 76,197
  • 13
  • 71
  • 125
0

You can do something like this:

var builder = Builders<yourType>.Filter;
var filter = builder.Eq("something", something);
var update = Builders<youType>.Update
    .Set("something", someNewThing)
    .SetOnInsert("something2", someNewThing2);
yourContext.yourCollection.UpdateManyAsync(filter, update, new UpdateOption {IsUpsert = true}).Result.IsAcknowledged;
Mahdi
  • 3,199
  • 2
  • 25
  • 35