当前位置: 动力学知识库 > 问答 > 编程问答 >

mongodb - 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" }

网友答案:

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})
分享给朋友:
您可能感兴趣的文章:
随机阅读: