Page / Slice containing unknown instances in spring's pagination and slicing over mongodb

Viewed 13936

When trying to use spring's pagination or slicing to iterate a very large mongodb collection, the program runs ok but at certain point the next page / slice is empty and in debug there is the message of "Page / Slice containing unknown instances".

Here is the code sample:

do {
  Pageable pageable = new PageRequest(pageNumber, PAGE_SIZE, sort);
  nextPage = catsRepository.findByTypeAndIdGreaterThan("street cat", lastId, 
          pageable);
  dbRecords = nextPage.getContent();
   if (dbRecords.size() > 0) {
       lastId = new ObjectId( doSomthing());
       pageNumber++; 

    }
 } while (nextPage.hasNext());

Also, when using different page size, it seems like the location of the problem is different:

After running with slice of size 1, problem at slice 133

After running with slice of size 2, problem at slice 98

After running with slice of size 3, problem at slice 76

Is there a malformed document in the DB that can cause this?

During the iteration I calculate the expected number of slices / pages and the loops stops before it reaches that number.

what can cause such a thing?

Thanks.

2 Answers

Normally it happens when you are requesting a pageNumber "out of bounds". Suppose you have 10 registers in db that you want and you define a PAGE_SIZE=10, so you have only one page and its index is 0, that means if you request for pageNumber=1, you will get containing unknown instances problem.

Summarizing, try for pageNumber=0.

Related