使用spring数据mongodb分配聚合管道的结果

时间:2017-05-19 10:21:13

标签: spring-boot spring-data-mongodb spring-restcontroller spring-rest

我在分页聚合管道的结果时遇到了一些麻烦。在看了In spring data mongodb how to achieve pagination for aggregation之后,我想出了一个感觉像是一个hacky解决方案。我首先执行匹配查询,然后按照我搜索的字段进行分组,并计算结果,将值映射到私有类:

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, Athlete.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;
    }
}

这样,我能够为我返回的页面对象提供“总计”值:

public Page<Athlete> findAllByName(String name, Pageable pageable) {
    long total = getCount("team.name", name);
    Aggregation aggregation = getAggregation("team.name", name, pageable);
    List<Athlete> aggregationResults = mongoTemplate.aggregate(
        aggregation, Athlete.class, Athlete.class
    ).getMappedResults();
    return new PageImpl<>(aggregationResults, pageable, total);
}

您可以看到,用于获取结果总数的聚合与我想要执行的实际聚合没有太大区别:

MatchOperation matchOperation = match(Criteria.where(propertyName).is(propertyValue));
SkipOperation skipOperation = skip((long) (pageable.getPageNumber() * pageable.getPageSize()));
LimitOperation limitOperation = limit(pageable.getPageSize());
SortOperation sortOperation = sort(pageable.getSort());
return newAggregation(matchOperation, skipOperation, limitOperation, sortOperation);

这绝对有效,但是,正如我所说的那样,感觉很糟糕。有没有办法获取PageImpl实例的计数而基本上不必运行两次查询?

1 个答案:

答案 0 :(得分:0)

您的问题帮助我解决了聚合时分页的相同问题,因此我进行了一些深入研究,并为您的问题提供了解决方案。我知道已经有点晚了,但是有人可能会用不上这个答案。我绝不是Mongo专家,所以如果我做的是不好的做法或表现不佳,请不要犹豫,让我知道。

使用组,我们可以将根文档添加到集合中并计数。

group().addToSet(Aggregation.ROOT).as("documents")
       .count().as("count"))

这是您几乎要解决的同样问题的我的解决方案。

private Page<Customer> searchWithFilter(final String filterString, final Pageable pageable, final Sort sort) {
    final CustomerAggregationResult aggregationResult = new CustomerAggregationExecutor()
        .withAggregations(match(new Criteria()
                .orOperator(
                    where("firstName").regex(filterString),
                    where("lastName").regex(filterString))),
            skip((long) (pageable.getPageNumber() * pageable.getPageSize())),
            limit(pageable.getPageSize()),
            sort(sort),
            group()
                .addToSet(Aggregation.ROOT).as("documents")
                .count().as("count"))
        .executeAndGetResult(operations);
    return new PageImpl<>(aggregationResult.getDocuments(), pageable, aggregationResult.getCount());
}

CustomerAggregationResult.java

@Data
public class CustomerAggregationResult {

  private int count;
  private List<Customer> documents;

  public static class PageableAggregationExecutor {

    private Aggregation aggregation;

    public CustomerAggregationExecutor withAggregations(final AggregationOperation... operations) {
      this.aggregation = newAggregation(operations);
      return this;
    }

    @SuppressWarnings("unchecked")
    public CustomerAggregationResult executeAndGetResult(final MongoOperations operations) {
        return operations.aggregate(aggregation, Customer.class, CustomerAggregationResult.class)
            .getUniqueMappedResult();
    }

  }
}

真的希望这会有所帮助。

编辑:我最初创建了一个带有List的通用PageableAggregationResult,但是当我通过PageableAggregationResult.class时,它返回一个IllegalArgumentException,它没有T的类型。如果我找到一个解决方案,我将编辑此答案,因为我希望最终汇总了多个集合。