title: Java, MongoDB: How to update every object while iterating a huge collection?
body: <p>I have a collection of about 1 million records with 20 fields each. I need to update integer <code>flag</code> field in every record (document) assigning randomly 1 or 2 to this <code>flag</code> 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:  </p>
  
  <pre><code>  DBCursor cursor = coll.find();
    try {
       while(cursor.hasNext()) {
      BasicDBObject obj = (BasicDBObject) cursor.next();
      ...
      coll.update(query,newObj)
  
       }
    } finally {
       cursor.close();
    }
  </code></pre>
  
  <p>How to update a field in every document of a huge MongoDB collection with different values efficiently? </p>