In spring data mongodb how to achieve pagination for aggregation

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

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

7条回答
  •  攒了一身酷
    2021-02-06 00:29

    To return a Paged Object with correct value of pageable object , I find this is the best and simple way.

    Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(Criteria.where("type").is("project")),
                            Aggregation.group("id").last("id").as("id"), Aggregation.project("id"),
                            Aggregation.skip(pageable.getPageNumber() * pageable.getPageSize()),
                            Aggregation.limit(pageable.getPageSize()));
    
    
        PageableExecutionUtils.getPage(mongoTemplate.aggregate(aggregation, Draft.class, Draft.class).getMappedResults(), pageable,() -> mongoTemplate.count(Query.of(query).limit(-1).skip(-1), Draft.class));
    

提交回复
热议问题