Does list operations load whole data at first call?

223 views Asked by At

Suppose I've got very big collection and I select it by

MongoCursor<MyClass> answer = myCollection.find().as(MyClass.class);  

Will Jongo/Mongo load whole collection at the first call or incrementally load data while I will iterate over answer?

1

There are 1 answers

0
K Erlandsson On BEST ANSWER

Jongo's MongoCursor uses Mongo's regular DBCursor under the hood. The DBCursor loads elements lazily (as typically all cursors do). I.e., your whole collection will not be loaded into memory, it will be lazily loaded while you iterate through your cursor.

Relevant source from Jongo where cursor is a DBCursor.

public E next() {
    if (!hasNext())
        throw new NoSuchElementException();

    DBObject dbObject = cursor.next();
    return resultHandler.map(dbObject);
}