Skip to main content

Pagination

MongoDB pages a find query with skip and limit. Count the same filter separately with count:

test.user_info.find({name: 'mali'}).sort({_id: 1}).skip(10).limit(10)
test.user_info.count({name: 'mali'})

dbVisitor Usage

Add a Page argument to a Mapper method. The entity mapping is shown in Entity Mapping.

@Query("test.user_info.find({name: #{name}}).sort({_id: 1})")
List<UserInfo> findByName(@Param("name") String name, Page page);
Page page = new PageObject(1, 10);
List<UserInfo> rows = mapper.findByName("mali", page);

Page numbering starts at zero. Page overrides skip/limit on find; a requested total triggers a separate count. For aggregate, write $skip and $limit explicitly.

Notes

Sort by a unique field to avoid ambiguous page order. fetchSize is not a page size, and preRead=false does not turn a query into pagination.

Counts and page reads are separate requests; concurrent writes can change their results. See 9.5 Pagination for the common API.

NULL ordering

The Builder API's OrderNullsStrategy.FIRST and LAST currently have no effect; DEFAULT uses MongoDB's default order. Do not rely on the two explicit options to move nulls to the beginning or end of paginated results.