Animesh Pandey
Animesh Pandey

Reputation: 6018

How can I retrieve large number of documents from Mongo datastore using Morphia?

I am using following code to iterate through a database's documents.

public void readDataStore() throws IOException {
        Query query = document_datastore.find(DocumentPojo.class);
        List<DocumentPojo> documentPojos = query.asList();

        documentPojos.forEach(obj -> {
                    try {
                        System.out.println(obj.getDocid());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
        );    
}

Currently the DB has not more than 100 documents but in future it can have ~100000 documents. I suspect then it may run into performance issues?

DocumentPojo is the class that I map the results to. I am using Java 8 and Morphia.

How should I solve this issue?

Upvotes: 2

Views: 1215

Answers (2)

srborlongan
srborlongan

Reputation: 4579

One way of implementing @evanchooly's answer:

public void readDataStore() throws IOException {
    final Query query = document_datastore.find(DocumentPojo.class);

    query.fetch().forEach(obj -> {
        try {
            System.out.println(obj.getDocid());
        } catch (final IOException e) {
            e.printStackTrace();
        }
    });    

}

Upvotes: 2

evanchooly
evanchooly

Reputation: 6243

Use query.fetch() to get the MorphiaIterator and then handle each document as you get it. It won't pull them all in to memory at once allowing you to process your hundred thousand+ documents.

Upvotes: 4

Related Questions