2015-09-25 78 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());