Java 使用 mongoTemplate 进行分页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29030542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pagination with mongoTemplate
提问by Marcin Wisnicki
I have a Query with Pageable:
我有一个带有 Pageable 的查询:
Query query = new Query().with(new PageRequests(page, size))
How can I execute it with MongoTemplate ? I don't see a single method returning Page<T>
.
如何使用 MongoTemplate 执行它?我没有看到一个方法返回Page<T>
。
采纳答案by Ori Dar
MongoTemplate
does not have methods to return Page
. The find()
methods return an ordinary List
.
MongoTemplate
没有方法返回Page
。这些find()
方法返回一个普通的List
.
with(new PageRequests(page, size)
is used internally to adjust skip
and limit
with a MongoDB query (proceeded by a count query I think)
with(new PageRequests(page, size)
在内部用于调整skip
和limit
使用 MongoDB 查询(我认为由计数查询进行)
Page
can be used in conjunction with MongoDB repositorieswhich is a specialized case of Spring data repositories.
Page
可以与MongoDB 存储库结合使用,MongoDB 存储库是 Spring 数据存储库的一个特例。
Thus, you'll have to use MongoRepository
's Page findAll(Pageable pageable)
for paginated results (actually inherited from PagingAndSortingRepository
).
因此,您必须将MongoRepository
'sPage findAll(Pageable pageable)
用于分页结果(实际上是从 继承的PagingAndSortingRepository
)。
回答by d0x
It's true that the MongoTemplate
doesn't have findXXX
with Pageables.
的确,PageablesMongoTemplate
没有findXXX
。
But you can use the Spring Repository PageableExecutionUtils
for that.
但是您可以PageableExecutionUtils
为此使用 Spring Repository 。
In your example it would look like this:
在您的示例中,它看起来像这样:
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));
Like in the original Spring Data Repository, the PageableExecutionUtils
will do a count request and wrap it into a nice Page
for you.
就像在最初的 Spring Data Repository 中一样,它PageableExecutionUtils
会做一个计数请求并将它包装成一个Page
对你来说很好的。
Hereyou can see that spring is doing the same.
在这里你可以看到 spring 也在做同样的事情。
回答by Razzlero
Based on d0x's answer and looking at the spring code. I'm using this variation which works off the spring-boot-starter-data-mongodb dependency without needing to add spring data commons.
基于 d0x 的回答并查看spring 代码。我正在使用这种变体,它可以消除 spring-boot-starter-data-mongodb 依赖项,而无需添加 spring 数据公共。
@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;
}
回答by velastiqui
return type Mono<Page<Myobject>>...
return this.myobjectRepository.count()
.flatMap(ptiCount -> {
return this.myobjectRepository.findAll(pageable.getSort())
.buffer(pageable.getPageSize(),(pageable.getPageNumber() + 1))
.elementAt(pageable.getPageNumber(), new ArrayList<>())
.map(ptis -> new PageImpl<Myobject>(ptis, pageable, ptiCount));
});