In spring data mongodb how to achieve pagination for aggregation

前端 未结 7 1508
渐次进展
渐次进展 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<T, ID> interface. Then, you can create an @Aggregation query method like this:

    @Aggregation(pipeline = {
          "{ $match: { someField: ?0 } }",
          "{ $project: { _id: 0, someField: 1} }"
    })
    List<StuffAggregateModel> 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.

    0 讨论(0)
  • 2021-02-06 00:10

    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:

    public Page<UserListItemView> list(final Pageable pageable) {
        long total = getCount(<your property name>, <your property value>);
    
        final Aggregation agg = newAggregation(
            skip(pageable.getPageNumber() * pageable.getPageSize()),
            limit(pageable.getPageSize())
        );
    
        final List<UserListItemView> results = mongoTemplate
            .aggregate(agg, User.class, UserListItemView.class)
            .getMappedResults();
    
        return new PageImpl<>(results, pageable, total);
    }
    

    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:

    private long getCount(String propertyName, String propertyValue) {
        MatchOperation matchOperation = match(Criteria.where(propertyName).is(propertyValue));
        GroupOperation groupOperation = group(propertyName).count().as("count");
        Aggregation aggregation = newAggregation(matchOperation, groupOperation);
        return mongoTemplate.aggregate(aggregation, Foo.class, NumberOfResults.class).getMappedResults().get(0).getCount();
    }
    
    private class NumberOfResults {
        private int count;
    
        public int getCount() {
            return count;
        }
    
        public void setCount(int count) {
            this.count = 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:

    private long getCount(String propertyName, String propertyValue) {
        Query countQuery = new Query(Criteria.where(propertyName).is(propertyValue));
        return mongoTemplate.count(countQuery, Foo.class);
    }
    
    0 讨论(0)
  • 2021-02-06 00:11

    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.

        AggregationOperation group = Aggregation.group().count().as("total")
                .addToSet(pageable.getPageNumber()).as("pageNumber")
                .addToSet(pageable.getPageSize()).as("pageSize")
                .addToSet(pageable.getOffset()).as("offset")
                .push("$$ROOT").as("data");
    

    Use Aggregation project to slice as per the paging information.

        AggregationOperation project = Aggregation.project()
                .andInclude("pageSize", "pageNumber", "total", "offset")
                .and(ArrayOperators.Slice.sliceArrayOf("data").offset((int) pageable.getOffset()).itemCount(pageable.getPageSize()))
                .as("data");
    

    Use mongo template to aggregate.

        Aggregation aggr = newAggregation(group, project);
        CustomPage page = mongoTemplate.aggregate(aggregation, Foo.class, CustomPage.class).getUniqueMappedResult();
    

    Create a CustomPage.

        public class CustomPage {
            private long pageSize;
            private long pageNumber;
            private long offset;
            private long total;
            private List<Foo> data;
        }
    
    0 讨论(0)
  • 2021-02-06 00:16

    Here is my generic solution:

    public Page<ResultObject> list(Pageable pageable) {
        // build your main stages
        List<AggregationOperation> mainStages = Arrays.asList(match(....), group(....));
        return pageAggregation(pageable, mainStages, "target-collection", ResultObject.class);
    }
    
    public <T> Page<T> pageAggregation(
            final Pageable pageable,
            final List<AggregationOperation> mainStages,
            final String collection,
            final Class<T> clazz) {
        final List<AggregationOperation> stagesWithCount = new ArrayList<>(mainStages);
        stagesWithCount.add(count().as("count"));
        final Aggregation countAgg = newAggregation(stagesWithCount);
        final Long count = Optional
                .ofNullable(mongoTemplate.aggregate(countAgg, collection, Document.class).getUniqueMappedResult())
                .map(doc -> ((Integer) doc.get("count")).longValue())
                .orElse(0L);
    
        final List<AggregationOperation> stagesWithPaging = new ArrayList<>(mainStages);
        stagesWithPaging.add(sort(pageable.getSort()));
        stagesWithPaging.add(skip(pageable.getOffset()));
        stagesWithPaging.add(limit(pageable.getPageSize()));
        final Aggregation resultAgg = newAggregation(stagesWithPaging);
        final List<T> result = mongoTemplate.aggregate(resultAgg, collection, clazz).getMappedResults();
    
        return new PageImpl<>(result, pageable, count);
    }
    
    0 讨论(0)
  • 2021-02-06 00:21

    In addition to ssouris solution you can use Pageable classes for the results.

    public Page<UserListItemView> list(final Pageable pageable) {
    
        final Aggregation agg = newAggregation(
            skip(pageable.getPageNumber() * pageable.getPageSize()),
            limit(pageable.getPageSize())
        );
    
        final List<UserListItemView> results = mongoTemplate
            .aggregate(agg, User.class, UserListItemView.class)
            .getMappedResults();
    
        return new PageImpl<>(results, pageable, results.size())
    }
    
    0 讨论(0)
  • 2021-02-06 00:23

    You can use MongoTemplate

    org.spring.framework.data.mongodb.core.aggregation.Aggregation#skip
            and 
    org.springframework.data.mongodb.core.aggregation.Aggregation#limit
    
    Aggregation agg = newAggregation(
            project("tags"),
            skip(10),
            limit(10)
    );
    
    AggregationResults<TagCount> results = mongoTemplate.aggregate(agg, "tags", TagCount.class);
    List<TagCount> tagCount = results.getMappedResults();
    
    0 讨论(0)
提交回复
热议问题