Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bulk update/upsert in MongoDB?

Is it possible to do bulk update/upsert (not insert) in MongoDB?

If yes, please point me to any docs related to this?

Thanks

like image 528
StackUnderflow Avatar asked Dec 14 '10 20:12

StackUnderflow


People also ask

What is Upsert in MongoDB update?

Upsert is a combination of insert and update (inSERT + UPdate = upsert). We can use the upsert with different update methods, i.e., update, findAndModify, and replaceOne. Here in MongoDB, the upsert option is a Boolean value. Suppose the value is true and the documents match the specified query filter.

How do you update Upsert?

Or in other words, upsert is a combination of update and insert (update + insert = upsert). If the value of this option is set to true and the document or documents found that match the specified query, then the update operation will update the matched document or documents.

How do I bulk write in MongoDB?

bulkWrite() Method. MongoDB is a versatile documentum based NoSQL database and has the ability to perform DB write operations efficiently by means of its bulkWrite() method. That means multiple documents can be inserted/updated/deleted in one shot. This method can be used in multi-document transactions.


2 Answers

You can use the command line program mongoimport it should be in your MongoDB bin dir ...

There are two options you'll want to look into to use upsert ...

--upsert insert or update objects that already exist
--upsertFields arg comma-separated fields for the query part of the upsert. You should make sure this is indexed

More info here: http://www.mongodb.org/display/DOCS/Import+Export+Tools

Or just do ...

$ mongoimport --help
like image 188
Justin Jenkins Avatar answered Sep 22 '22 15:09

Justin Jenkins


mongo can execute .js file. you can push all you update commands in a js file.

t.js

db.record.update({md5:"a35f10a8339ab678612d1f86be08b81a"},{$set:{algres:[]}},false,true);
db.record.update({md5:"a35f10a8339ab678612d1f86be08b81b"},{$set:{algres:[]}},false,true);

then, mongo 127.0.0.1/test t.js

like image 30
unique Avatar answered Sep 24 '22 15:09

unique