2015-09-25 68 views
2

我正在使用spring mongo模板在mongodb上运行协调查询。我想知道是否有任何方法可以找出春季mongo模板中聚合结果的计数?如何在spring mongo模板中获取聚合查询次数

这里是我的聚集样本:

Aggregation agg = newAggregation(Class1.class, 
      match(criteria), 
      group("prop1", "prop2") 
      .avg("x").as("averageX") 
     ); 

我只需要知道如何得到这个聚合结果的数量在春季蒙戈模板。

+0

是的,有。但是这里也没有代码来显示你在做什么。 –

+0

我只是使用Spring Aggregation Class进行聚合。我只需要在得到结果之前得到结果数。 – zhozhe

+0

听起来像你想要一个“方面”的结果,然后与分页,如Solr/ElasticSearch。使用两个单独的查询。这是做到这一点的正确方法。否则,只需计算结果的数量。那有什么问题?再次。仍然没有代码。清除泥浆。只是猜测 –

回答

1

我的回应来得非常晚,但它可能会帮助其他人。要获得聚合的计数,您需要在最后添加一个新组:

在聚合结束时添加 - > Aggregation.group()。count()。as(“count”)以获取算上

Aggregation aggregation = newAggregation(
      Aggregation.match(Criteria.where("x").is(x).and("y").exists(true)), 
      Aggregation.group("x", "y"), 
      Aggregation.group().count().as("count") 
); 

得到计数:

Long.parseLong(results.getMappedResults().get(0).get("count").toString()); 
相关问题