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?