I am trying to update every documents "Name" field in the collection using the iterator() defined in the FindIterable interface in Java Mongo driver. The next() function on the iterator should give me the next BSON object, but actually, it's iterating over the same object.
public void func1(FindIterable<org.bson.Document> documents, MongoCollection<org.bson.Document> coll_name) {
/*
* This function will run until all the documents in a collection aren't retrieved.
* */
try {
while (documents.iterator().hasNext()) {
coll_name.updateOne(eq("_id", documents.iterator().next().get("_id")), new org.bson.Document("$set", new org.bson.Document("Name", getName((String) documents.iterator().next().get("NameSource")))));
System.out.println("Document _id " + documents.iterator().next().get("_id") + " updated.....! in time : " + df.format(dater));
}
}catch (Exception ex){
System.out.println(" ~~~~~~~~~~~Was not able to getName() & update document~~~~~~~~~~~~~~~~~~");
System.out.println(ex.getMessage()) ;
} finally {
documents.iterator().close();
}
}
Call to the function:
FindIterable<org.bson.Document> FRESH_docs = coll_name.find(exists("Text", false)).noCursorTimeout(true);
int flag = 1;
try{
func1(FRESH_docs, coll_name, flag);
}catch (Exception ex){
System.out.println(" ~~~~~~~~~~~call to func1() failed for FRESH_docs ~~~~~~~~~~~~~~~~~~");
System.out.println(ex.getMessage());
}
The output is:
Document _id 4713205 updated.....! in time : 2017-12-25 08:56:42.876
Document _id 4713205 updated.....! in time : 2017-12-25 08:56:42.902
Document _id 4713205 updated.....! in time : 2017-12-25 08:56:42.930
Document _id 4713205 updated.....! in time : 2017-12-25 08:56:42.958
Document _id 4713205 updated.....! in time : 2017-12-25 08:56:42.984
Document _id 4713205 updated.....! in time : 2017-12-25 08:56:43.012
.....
I removed the date-time printers for clean code evaluation. Can anyone suggest what's the mistake I am doing that's iterating over the same BSON document?