Search code examples
springmongodbpaginationspring-data-mongodbmongorepository

MongoRepository paging request returning incorrect data


I have a simple paging request over Spring MongoRepository but MongoRepository apparently starts sending incorrect result after some arbitrary high page#. I am posting here to know if I am missing something or this could be a bug with Spring MongoRepository.

In my test i have 14 elements in my test mongo db and below paging requests works fine (retrieves data if its there): pageSize: 10, page#: 0 to 1073741823 But below page request returns me 10 entities from my mongoDB which i am not expecting: pageSize: 10, page#: 1073741824

The breaking point is diff for diff combination of pageSize. Can share if require.

Below the libraries i am using:

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>1.5.5.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>2.12.5</version>
    </dependency>

Repository definition:

public interface SomeEntityRepository extends MongoRepository<SomeEntity, Integer> {
}

And using this Repository as below:

public Page<SomeEntity> getSomeEntitiesByPage(int pageIndex, int paseSize, Map<String, Sort.Direction> sortQuery) {

    Pageable pageableRequest = new PageRequest(pageIndex, pageSize);

        return someEntityRepository.findAll(pageableRequest);

}

Solution

  • This is an issue with Spring MongoRepository and the Jira has been filed for the same. MongoRepository is trying to get the required offset Pageable.getOffset() with return type as int and when pageNumber*pageSize > Integer.MaxValue it gets wrapped around as a negative offset causing the 1st page to be retrieved. Jira reference for this can be found here