Bulk update/upsert in MongoDB?

三世轮回 提交于 2019-12-06 20:12:30

问题


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

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

Thanks


回答1:


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



回答2:


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




回答3:


Bulk updates can also be done in batches as found in the documentation:

MongoDB Bulk Methods

I use these to import CSV files that I need to massage a bit before importing the data. Its kinda slow when dealing with updates, but it did my 50K document updates in about 83 seconds, which is far slower than mongoimport command.



来源:https://stackoverflow.com/questions/4444023/bulk-update-upsert-in-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!