Siva Sai
Siva Sai

Reputation: 396

Upsert query for multiple records

In MongoDB, how do I upsert multiple files at a time which are in json format?

I am able to upsert a single record by using

db.sample.update(
    { name: "harish" },
    { 
        $set: { "size": 2015 }, 
        $setOnInsert: { "name": "hi" }
    },
    { upsert: true }
)

But how do I upsert a json file which contains millions of records?

{ "name" : "mouli", "company" : "jp morgan", "size" : 40, "role" : "architect" } { "name" : "siva", "company" : "IBM", "size" : 30, "role" : "architect" } { "name" : "siva sai", "company" : "cisco", "size" : 220, "role" : "CEO" } { "name" : "siva{ "name" : "durga prasad", "company" : "big datamatica", "size" : 50, "role" : "CEO" } sai varma", "company" : "karbon", "size" : 50, "role" : "trainer" } { "name" : "jagan", "company" : "amazon", "size" : 500, "role" : "developer" }

Upvotes: 0

Views: 193

Answers (1)

Puneet Singh
Puneet Singh

Reputation: 3543

Upsert in mongodb means to update the record matching the condition, and if mongodb don't find any record matching condition it will insert a new document with values you mentioned in query.

Means it can update multiple records at once, but it will insert only one record if it cant find the document matching condition.

One more thing, you need to add multi: true in options to update multiple records.

db.sample.update({name: "harish"},{$set: {"size": 2015 }, $setOnInsert: { "name": "hi"}},{multi:true, upsert:true})

Upvotes: 1

Related Questions