I was wondering Spring Mongo API
for find
loads everything in a List
. If search result contains billion records, would it not affect memory? Can someone suggest a better way of achieving this without loading all this in memory. Using limit can help but then there is a flaw that it would not know if a new document is inserted in the collection. Well, find by limit would have the same effect if the collection would have modified after reading X of billion
records.
So two questions:
- Improve performance by not loading everything in memory
- How would you solve this un-known document added during processing?
Code from API
List<T> result = new ArrayList<T>();
while (cursor.hasNext()) {
DBObject object = cursor.next();
result.add(objectCallback.doWith(object));
}