这是我目前在Spring Data MongoDB上的分页方法:
@RequestMapping(value="/nextposts", method=RequestMethod.GET)
public List getNextPosts(@RequestParam int next) {
Pageable pageable = new PageRequest(next, 5, new Sort(new Sort.Order(Direction.DESC, "id")));
page = repo.findAll(pageable);
return page.getContent();
}
如您所见,我正在获取“next”变量指定的页面,然后将其返回给用户 .
我正在尝试处理页面中返回的最后一个文档,特别是获取它的ObjectId,以便我可以使用mongodb实现"Approach 2"快速分页:http://blog.mongodirector.com/fast-paging-with-mongodb/
我查看了PageRequest和Pageable的文档,但没有一个方法会返回页面中的最后一个文档 .
有谁知道如何做到这一点?
2 回答
Simon实际上你必须定制一些可分页的逻辑以获得更多的“功能”,因为我不认为最后一项可以像这样 .
这是一个很好的例子,您可以定义适当的页面资源模型并为其添加一些元数据 . 链接,如first,last,NEXT rels .
http://www.javaenthusiasm.com/rest-pagination-with-spring-boot/
如果您需要更多解释,请告诉我 .
最终我无法用MongoRepository做到这一点 .
我不得不使用MongoOperation / MongoTemplate来完成这项任务 .