Java, MongoDB: How to update every object while iterating a huge collection?

倖福魔咒の 提交于 2019-12-18 13:03:30

问题


I have a collection of about 1 million records with 20 fields each. I need to update integer flag field in every record (document) assigning randomly 1 or 2 to this flag field. How to do this while iterating cursor over the complete collection? It does not seem to be a good idea to search second time for object already found by MongoDB just to be able to update it:

  DBCursor cursor = coll.find();
  try {
     while(cursor.hasNext()) {
    BasicDBObject obj = (BasicDBObject) cursor.next();
    ...
    coll.update(query,newObj)

     }
  } finally {
     cursor.close();
  }

How to update a field in every document of a huge MongoDB collection with different values efficiently?


回答1:


Your approach is basically correct. However I wouldn't consider such a collection as "huge" You can run something similar from the shell:

coll.find({}).forEach(function (doc) {
    doc.flag = Math.floor((Math.random()*2)+1);
    coll.save(doc);
 });

Depending on your MongoDB version, configuration and load, this may take something between few minutes to several hours

If you want to perform this update in bulks, use some conditions in your query document, something such as coll.find({"aFiled" : {$gt : minVal}, "aFiled" : {$lt : maxVal}})




回答2:


My solution to my own question, inspired by @orid :

public void tagAll(int min, int max) {
    int rnd = 0;
    DBCursor cursor = this.dataColl.find();
    try {
        while (cursor.hasNext()) {
            BasicDBObject obj = (BasicDBObject) cursor.next();
            rnd = min + (int) (Math.random() * ((max - min) + 1));
            obj.put("tag", rnd);
            this.dataColl.save(obj);
        }
    } finally {
        cursor.close();
    }
}


来源:https://stackoverflow.com/questions/15969592/java-mongodb-how-to-update-every-object-while-iterating-a-huge-collection

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