user1965449
user1965449

Reputation: 2931

MongoDB Java Driver Infinite loop while reading results

I am reading MongoDB results using the following code , however the while loop runs in an infinite loop always iterating over the first element in the collection, can someone point out what I am doing wrong.

       Iterable<DBObject> list = playerData.results();
        if(list != null){
            while(list.iterator().hasNext()) {

                DBObject obj = list.iterator().next();
                DBObject id =  (DBObject) obj.get("_id");
                String player= obj.get("player").toString();
                //Populate the memcached here .
                PlayerDTO rcd = new PlayerDTO();

                if(id != null && id.get("venue" != null && id.get("score") != null) {

                    rcd.setVenue(id.get("venue").toString());
                    rcd.setScore(new Double(id.get("score").toString()).doubleValue());
                }

            }
        }

Upvotes: 0

Views: 683

Answers (1)

Rick Mangi
Rick Mangi

Reputation: 3769

You are reassigning the original iterator to the while() loop each iteration through.

Iterator i = list.iterator();
while(i.hasNext()) {
  ....
}

Upvotes: 2

Related Questions