Reputation: 13471
I´m trying to make it works pagination with spring data mongodb, but I found that the first imt eworks, but if I increase the page number in the pagination return me always 0 records. Here´s my code:
The code in my Service
@Override
public List<Purchase> findByUserId(String userId, int pageNumber) {
Pageable request =
new PageRequest(pageNumber - 1, PAGE_SIZE);
return purchaseRepository.findByUserId(userId, request).getContent();
}
And here the repository
@Component
public interface PurchaseRepository extends MongoRepository<Purchase, Integer> {
Purchase findById(@Param("id") String id);
Page<Purchase> findByUserId(@Param("userId") String userId,Pageable pageable);
List<Purchase> findAll();
}
But like I said, when pageNumber is 1 everything is ok. But when is 10 the second time the result is 0 PAGE_SIZE = 10 and yes, I already check the database and I already have 15 documents.
Any idea fellas?
Upvotes: 1
Views: 12148
Reputation: 239
If you have 15 documents, with pageSize=10, you will only have data in page# 0 and 1 containing 10 and 5 elements respectively. Subsequent pages should fetch you no objects
Upvotes: 2