Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate over large collection in MongoDB via spring-data

Friends!

I am using MongoDB in java project via spring-data. I use Repository interfaces to access data in collections. For some processing I need to iterate over all elements of collection. I can use fetchAll method of repository, but it always return ArrayList.

However, it is supposed that one of collections would be large - up to 1 million records several kilobytes each at least. I suppose I should not use fetchAll in such cases, but I could not find neither convenient methods returning some iterator (which may allow collection to be fetched partially), nor convenient methods with callbacks.

I've seen only support for retrieving such collections in pages. I wonder whether it is the only way for working with such collections?

like image 906
Rodion Gorkovenko Avatar asked Jun 15 '12 07:06

Rodion Gorkovenko


People also ask

Can I use Spring data JPA with MongoDB?

Yes, DataNucleus JPA allows it, as well as to many other databases.

Which is better MongoTemplate or MongoRepository?

So I'd say that MongoTemplate is a better option, unless you have a very elaborated POJO model or need the custom queries capabilities of MongoRepository for some reason. Good points/examples. However your race condition example and undesired result can be avoided using @Version to prevent that very scenario.

What is the difference between MongoOperations and MongoTemplate?

MongoTemplate provides a simple way for you to save, update, and delete your domain objects and map those objects to documents stored in MongoDB. You can save, update and delete the object as shown below. MongoOperations is the interface that MongoTemplate implements.

Is MongoDB good with spring boot?

With Spring boot, we can quickly create stand-alone applications without having to make too many configuration changes (as we will see later). MongoDB is the most popular NoSQL database because of the ease with which data can be stored and retrieved.


1 Answers

Late response, but maybe will help someone in the future. Spring data doesn't provide any API to wrap Mongo DB Cursor capabilities. It uses it within find methods, but always returns completed list of objects. Options are to use Mongo API directly or to use Spring Data Paging API, something like that:

        final int pageLimit = 300;         int pageNumber = 0;         Page<T> page = repository.findAll(new PageRequest(pageNumber, pageLimit));         while (page.hasNextPage()) {             processPageContent(page.getContent());             page = repository.findAll(new PageRequest(++pageNumber, pageLimit));         }         // process last page         processPageContent(page.getContent()); 

UPD (!) This method is not sufficient for large sets of data (see @Shawn Bush comments) Please use Mongo API directly for such cases.

like image 191
udalmik Avatar answered Sep 18 '22 19:09

udalmik