In spring data mongodb using mongotemplate or mongorepository, how to achieve pagination for aggregateion
相关问题
- Spring Data MongoDB - lazy access to some fields
- Dependency injection into Logback Appenders with S
- Deserialize duplicate keys to list using Jackson
- How can I access the repository from the entity in
- Prevent Swagger from automatically adding some mod
相关文章
- How to load @Configuration classes from separate J
- Using Spring Dynamic Languages Support from Groovy
- Spring JMS : Set ErrorHandler for @JmsListener ann
- ModelMapper: Choose mapping based on Child class
- Configure Spring for CORS
- Remove transitive classpath dependency in gradle
- SpringBoot When file upload size limit exceeds get
- Can we add a feature module to an Angular app afte
To return a Paged Object with correct value of pageable object , I find this is the best and simple way.
In addition to ssouris solution you can use Pageable classes for the results.
This is an answer to an old post, but I'll provide an answer in case anyone else comes along while searching for something like this.
Building on the previous solution by Fırat KÜÇÜK, giving the results.size() as the value for the "total" field in the PageImpl constructor will not making paging work the way, well, you expect paging to work. It sets the total size to the page size every time, so instead, you need to find out the actual total number of results that your query would return:
Now, then, the best way to get the total number of results is another question, and it is one that I am currently trying to figure out. The method that I tried (and it worked) was to almost run the same aggregation twice, (once to get the total count, and again to get the actual results for paging) but using only the MatchOperation followed by a GroupOperation to get the count:
It seems kind of inefficient to run nearly the same query twice, but if you are going to page results, the pageable object must know the total number of results if you really want it to behave like paging. If anyone can improve on my method to get the total count of results, that would be awesome!
Edit: This will also provide the count, and it is simpler because you do not need a wrapper object to hold the result, so you can replace the entire previous code block with this one:
You can use MongoTemplate
As per the answer https://stackoverflow.com/a/39784851/4546949 I wrote code for Java.
Use aggregation group to get count and array of data with other paging information.
Use Aggregation project to slice as per the paging information.
Use mongo template to aggregate.
Create a CustomPage.