简体   繁体   中英

Insert/Update multiple records

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?

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);

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM