spring data mongodb中如何实现分页聚合 [英] In spring data mongodb how to achieve pagination for aggregation

查看:109
本文介绍了spring data mongodb中如何实现分页聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

spring data mongodb使用mongotemplate或mongorepository,如何实现分页聚合

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

推荐答案

这是对旧帖子的回答,但我会提供答案,以防其他人在搜索此类内容时出现.

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.

基于之前的 Fırat KÜÇÜK 的解决方案,将 results.size() 作为total" PageImpl 构造函数中的字段不会使分页工作的方式,好吧,您希望分页工作.它每次都将总大小设置为页面大小,因此您需要找出查询将返回的实际结果总数:

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);
}

那么,获得结果总数的最佳方法是另一个问题,而我目前正试图弄清楚这个问题.我尝试(并且有效)的方法是几乎两次运行相同的聚合(一次获得总计数,再次获得分页的实际结果)但仅使用 MatchOperation 后跟 GroupOperation 来获得计数:

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);
}

这篇关于spring data mongodb中如何实现分页聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆