这是我当前在 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 实现快速分页的“方法 2”:http://blog.mongodirector.com/fast-paging-with-mongodb/
我查看了 PageRequest 和 Pageable 的文档,但没有一个方法会返回页面中的最后一个文档。
有人知道如何做到这一点吗?
最佳答案
最终我无法使用 MongoRepository 做到这一点。
我必须使用 MongoOperation/MongoTemplate 来完成此任务。
@RequestMapping(value="/XXX", method=RequestMethod.GET)
public List getNextPosts(@RequestParam String next) {
Query query = new Query();
query.with(new Sort(Sort.Direction.DESC, "_id"));
query.limit(5);
ObjectId objID = new ObjectId(next);
query.addCriteria(Criteria.where("_id").lt(objID));
List<Posts> posts = mongoOperation.find(query, Posts.class);
return posts;
}
关于java - Spring Data Mongodb - 获取页面上的最后一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30809518/