简体   繁体   English

Java,MongoDB:如何在迭代庞大的集合时更新每个对象?

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

I have a collection of about 1 million records with 20 fields each. 我收集了大约100万条记录,每条记录有20个字段。 I need to update integer flag field in every record (document) assigning randomly 1 or 2 to this flag field. 我需要更新每个记录(文档)中的整数flag字段,随机分配1或2到此flag字段。 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: 为了能够更新它,第二次搜索MongoDB已找到的对象似乎不是一个好主意:

  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? 如何有效地更新具有不同值的巨大MongoDB集合的每个文档中的字段?

Your approach is basically correct. 你的方法基本上是正确的。 However I wouldn't consider such a collection as "huge" You can run something similar from the shell: 但是我不认为这样的集合是“巨大的”你可以从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 根据您的MongoDB版本,配置和负载,这可能需要几分钟到几个小时

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}}) 如果要在批量处理中执行此更新,请在查询文档中使用某些条件,例如coll.find({"aFiled" : {$gt : minVal}, "aFiled" : {$lt : maxVal}})

My solution to my own question, inspired by @orid : 我对@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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM