Vipin
Vipin

Reputation: 938

Mongodb Update or insert in c#

I want to update or insert into to mongo collection "Member". Under this collection i have an array MagazineSubscription. Here magazine Code is unique. Please refer the sample JSON.

So if need to update or insert into mongo using C# mongo driver.

  1. First I need to check this code exist 2, If it exist update one
  2. If it does not exist insert.

Is there any way I can do in one step. Like if it already exist update otherwise insert. Instead of hit twice. Because my collection is very big.

{ 
    "_id" : ObjectId("5c44f7017en0893524d4e9b1"), 
    "Code" : "WH01", 
    "Name" : "Lara", 
    "LastName" : "John", 
    "DOB" : "12-10-2017", 
    "Gender" : "Male", 
    "Dependents" : [
        {
            "RelationShip" : "Son", 
            "Name" : "JOHN", 
            "DOB" : "01-01-1970", 
            "Gender" : "Male", 
            "Address" : "Paris", 
            "ContactNumber" : "+312233445666"
        }, 
        {
            "RelationShip" : "Wife", 
            "Name" : "Marry", 
            "DOB" : "01-01-1980", 
            "Gender" : "Female", 
            "Address" : "Paris", 
            "ContactNumber" : "+312233445666"
        }
    ]
    "Matrimony" : [
        {
            "Fee" : 1000.0, 
            "FromDate" : "01-01-2015", 
            "ToDate" : "01-01-2017", 
            "Status" : false
        }
    ], 
    "MagazineSubscription" : [
        {
            "MagazineCode" : "WSS", 
            "DateFrom" : "01-05-2018", 
            "DateTo" : "01-01-2020", 
            "PaidAmount" : 1000.0, 
            "ActualCost" : 1500.0, 
            "Status" : false, 
            "DeliveryStatus" : [
                {
                    "ReturnedDate" : "10-01-2019", 
                    "Comment" : "Returned because of invalid address"
                }, 
                {
                    "ReturnedDate" : "10-02-2019", 
                    "Comment" : "Returned because of invalid address"
                }
            ]
        }
    ]
}

Upvotes: 0

Views: 13930

Answers (1)

Manoj Vadehra
Manoj Vadehra

Reputation: 846

Use mongodb's update operation with upsert:true. Please refer here: https://docs.mongodb.com/manual/reference/method/db.collection.update/

Here's a sample from the page:

 db.collection.update(
   <query>,
   <update>,
   {
     upsert: <boolean>, //you need this option
     multi: <boolean>,
     writeConcern: <document>,
     collation: <document>,
     arrayFilters: [ <filterdocument1>, ... ]
   }
)

And here's a similar question according to what you need: Upserting in Mongo DB using official C# driver

EDIT 1

Steps:

  1. First you need to write a filter to scan if the document exists. you can check any number of keys (Essentially a document).
  2. Write the update section with the keys you'd like to update (Essentially a document).
  3. Set upsert to true.

Mongodb will use your filter to search the document. If found, it will use the update section to perform the update mentioned by you.

In case the document does not exist, a new document will be created by using the filter keys + keys in the update part.

Hope that makes things clear as I have never used a C# mongo driver. So I won't be able to provide you the exact syntax.

EDIT 2

I'm providing @jeffsaracco's solution here:

MongoCollection collection = db.GetCollection("matches");
var query = new QueryDocument("recordId", recordId); //this is the filter

var update = Update.Set("FirstName", "John").Set("LastName","Doe"); //these are the keys to be updated
matchCollection.Update(query, update, UpdateFlags.Upsert, SafeMode.False);

Upvotes: 0

Related Questions