简体   繁体   English

SpringData MongoDB分页不起作用

[英]SpringData MongoDB pagination not working

im here because i don't how to handle this. 我在这里,因为我不怎么处理。

I need to paginate the results of a query to a collection in mongodb. 我需要将查询结果分页到mongodb中的集合。

here is my custom query in the repository: 这是我在存储库中的自定义查询:

@Query("{'data.consumer': ?0}.skip(?1).limit(?2)")
List<JSONObject> findAll(String consumer, int size, int page);

and here my test method: 这是我的测试方法:

@Test
public void testPaginationResult() {
    List<JSONObject> vehicleCaches;
    for (Integer i = 0; i < 5000; i += 400) {
        vehicleCaches = cacheRepository.findAll("collectionName", i, 400);
        System.out.println(vehicleCaches.size());
    }
}

whene i run the test the output of the system out is always 5000 (the total number of the documents in the collection). 当我运行测试时,系统的输出始终为5000(集合中文档的总数)。 It seems like the parameters "size" and "limit" are ignored. 似乎参数“大小”和“限制”被忽略了。

I also tried to use the Class PageRequest, but in that case, the pagination works ALWAYS in the first iteration, retrieving correctly 400 documents, but after the first iteration, always 0 documents are returns. 我也尝试使用Class PageRequest,但是在这种情况下,分页在第一次迭代中始终有效,可以正确检索400个文档,但是在第一次迭代之后,始终返回0个文档。

here is the code with PageRequest: 这是PageRequest的代码:

 for (Integer i = 0; i < total; i += 400) {
vehicleCaches = cacheRepository.findAll(arguments.get("collection"), PageRequest.of(i, 400));

} }

I also tried the same code above with with 我也尝试了与上面相同的代码

New PageRequest(i,400);

I use 我用

  • spring data mongodb 2.0.8.RELEASE 春季数据mongodb 2.0.8.RELEASE
  • spring 5.0.7.RELEASE 春天5.0.7.RELEASE
  • java 10.0.1 Java 10.0.1

Solved using paging with this solution 通过分页解决此问题

Page<JSONObject> vehicleCachesPaging;
vehicleCachesPaging = cacheRepository.findAll("COLLECTION", PageRequest.of(j, 400, Sort.unsorted()));

j is the for loop index. j是for循环索引。

j= 1,2,3,4,5......end j = 1,2,3,4,5 ...结束

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM