Java mongoTemplate分页
我有一个可分页的查询:Java mongoTemplate分页,java,spring-data,spring-data-mongodb,Java,Spring Data,Spring Data Mongodb,我有一个可分页的查询: Query query = new Query().with(new PageRequests(page, size)) 如何使用MongoTemplate执行它?我没有看到一个方法返回页面MongoTemplate没有方法返回页面。find()方法返回一个普通的列表 with(new PageRequests(page,size)在内部用于通过MongoDB查询调整跳过和限制(我认为是通过计数查询进行的) Page可以与Spring数据存储库的一个特例结合使用 因此,
Query query = new Query().with(new PageRequests(page, size))
如何使用MongoTemplate执行它?我没有看到一个方法返回
页面
MongoTemplate
没有方法返回页面
。find()
方法返回一个普通的列表
with(new PageRequests(page,size)
在内部用于通过MongoDB查询调整跳过和限制(我认为是通过计数查询进行的)
Page
可以与Spring数据存储库的一个特例结合使用
因此,您必须使用MongoRepository
的页面findAll(Pageable Pageable)
对分页结果进行分页(实际上是从分页和排序存储库继承的)。确实MongoTemplate
没有findXXX
但是您可以使用Spring存储库PageableExecutionTils
来实现这一点
在您的示例中,它将如下所示:
Pageable pageable = new PageRequests(page, size);
Query query = new Query().with(pageable);
List<XXX> list = mongoTemplate.find(query, XXX.class);
return PageableExecutionUtils.getPage(
list,
pageable,
() -> mongoTemplate.count(Query.of(query).limit(-1).skip(-1), XXX.class));
Pageable Pageable=新页面请求(页面,大小);
Query Query=new Query().with(可分页);
List=mongoTemplate.find(查询,XXX.class);
返回PageableExecutionUtils.getPage(
列表
可分页,
()->mongoTemplate.count(Query.of(Query.limit(-1).skip(-1),XXX.class));
与原始Spring数据存储库一样,pageableexecutionils
将执行一个计数请求,并将其包装成一个漂亮的页面
您可以看到spring也在做同样的事情。基于d0x的答案并查看。我正在使用这个变体,它可以在不需要添加spring数据公用项的情况下关闭spring启动程序数据mongodb依赖项
@Autowired
private MongoOperations mongoOperations;
@Override
public Page<YourObjectType> searchCustom(Pageable pageable) {
Query query = new Query().with(pageable);
// Build your query here
List<YourObjectType> list = mongoOperations.find(query, YourObjectType.class);
long count = mongoOperations.count(query, YourObjectType.class);
Page<YourObjectType> resultPage = new PageImpl<YourObjectType>(list , pageable, count);
return resultPage;
}
@Autowired
私有MongoOperations MongoOperations;
@凌驾
公共页面搜索自定义(可分页可分页){
Query Query=new Query().with(可分页);
//在这里构建您的查询
List=mongoOperations.find(查询,YourObjectType.class);
long count=mongoOperations.count(查询,YourObjectType.class);
页面结果页面=新页面导入(列表、可分页、计数);
返回结果页;
}
返回类型Mono。。。
返回此.myobjectRepository.count()
.flatMap(ptiCount->{
返回此.myobjectRepository.findAll(pageable.getSort())
.buffer(pageable.getPageSize(),(pageable.getPageNumber()+1))
.elementAt(pageable.getPageNumber(),new ArrayList())
.map(ptis->new PageImpl(ptis,可分页,ptiCount));
});
默认情况下,spring mongo模板没有按页面查找的方法。它搜索并返回整个记录列表。我尝试了这个方法,它成功了:
Pageable pageable = new PageRequests(0, 10);
Query query = new Query(criteria);
query.with(pageable);
List<User> lusers = mt.find(query, User.class);
Page<User> pu = new PageImpl<>(lusers, pageable, mongoTemplate.count(newQuery(criteria), User.class));
Pageable-Pageable=新的页面请求(0,10);
查询=新查询(条件);
查询.with(可分页);
List lusers=mt.find(查询,User.class);
Page pu=new PageImpl(lusers,pageable,mongoTemplate.count(newQuery(criteria),User.class));
类PageableExecutions在哪里?来自Spring Data commons:这不会导致查询在MongoDB端运行两次,并增加网络开销吗?@checklist是的,查询运行两次。因为查询应该针对索引集合运行,这应该不是什么大问题。好的,如果你是facebook或twitter,这可能是一个大问题deal;D.我没有其他解决方案这很有趣,但如果您的查询返回数十万或数百万行而没有分页,则当服务器尝试将所有这些文档读入内存时,这将使服务器瘫痪。使用聚合框架更好仅供参考:为了获得正确的计数值-查询应重置:跳过,限制值。计数调用应为:mongoOperations.count(query.skip(-1).limit(-1),YourObjectType.class)
。否则,它将为查询返回不正确的计数。
Pageable pageable = new PageRequests(0, 10);
Query query = new Query(criteria);
query.with(pageable);
List<User> lusers = mt.find(query, User.class);
Page<User> pu = new PageImpl<>(lusers, pageable, mongoTemplate.count(newQuery(criteria), User.class));