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

前端 未结 2 1436
面向向阳花
面向向阳花 2020-12-31 05:31

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

相关标签:
2条回答
  • 2020-12-31 05:57

    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}})

    0 讨论(0)
  • 2020-12-31 06:15

    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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题