In spring data mongodb how to achieve pagination for aggregation

前端 未结 7 1507
渐次进展
渐次进展 2021-02-05 23:32

In spring data mongodb using mongotemplate or mongorepository, how to achieve pagination for aggregateion

7条回答
  •  清酒与你
    2021-02-06 00:09

    Another approach would be to extend the PagingAndSortingRepository interface. Then, you can create an @Aggregation query method like this:

    @Aggregation(pipeline = {
          "{ $match: { someField: ?0 } }",
          "{ $project: { _id: 0, someField: 1} }"
    })
    List aggregateStuff(final String somePropertyName, final Pageable pageable);
    

    Just call this from your business logic service class and construct the Pageable (which also contains sort options, if desired) and call the repo method. I like this approach because of the simplicity and the sheer minimization of the amount of code that you have to write. If your query (aggregation pipeline) is simple enough, this is probably the best solution. Maintenance coding for this approach is nearly effortless.

提交回复
热议问题