3

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?

fedor.belov
  • 22,343
  • 26
  • 89
  • 134
  • Are you concerned about the memory usage of MongoDB (server side) or of Jongo (client side) ? – Sylvain Leroux Jun 09 '15 at 05:20
  • I'm concerned about client-side memory usage - I don't want to load whole collection into app memory – fedor.belov Jun 09 '15 at 05:26
  • 1
    *GetCollection doesn't load the collection, not even a Find() will. In fact, you'll have to start iterating the MongoCursor before anything is actually loaded from the database, and even then, it won't load the entire collection but only batches of configurable size* [Answered Here](http://stackoverflow.com/questions/28298949/does-mongodb-getcollection-method-load-the-entire-collection-into-ram-or-a-refer) – Kenneth Clark Jun 09 '15 at 05:53

1 Answers1

2

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);
}
K Erlandsson
  • 13,408
  • 6
  • 51
  • 67