简体   繁体   中英

How to Bulk Insert and update record in mongodb using c#

I have on requirement in that i want to read bulk record from Mongo DB A and Want to Insert and update based on the record exist or not in DB B.

Suppose in DB A i have 10 record initially so these record should be inserted in DB B. But after some time if some record are got updated in DB A and already as i have inserted in DB B. Now it should fire bulk update only.

Let me know if anyone having any idea about it using c#

You can take advantage of the $merge aggregation pipeline stage for this ( https://docs.mongodb.com/manual/reference/operator/aggregation/merge/ ).

This is easy to execute in C# but depending on what you need to achieve you can configure the parameters accordingly

var client = new MongoClient();
var databaseA = client.GetDatabase("A");
var databaseB = client.GetDatabase("B");

var collectionA = databaseA.GetCollection<BsonDocument>("test");
var collectionB = databaseB.GetCollection<BsonDocument>("test");
await collectionA.Aggregate()
    .Merge(collectionB)
    .ToListAsync();

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