如何在Spring Mongo模板中获取聚合查询计数 [英] How to get Count of aggregation query in spring mongo template
问题描述
我正在使用Spring mongo模板在mongodb上运行协议查询.我想知道有什么方法可以找出Spring mongo模板中聚合结果的计数吗?
I'm using spring mongo template to run an agreegation query on mongodb. I'm wondering is there any way to find out the count of aggregation result in spring mongo template?
这是我的汇总样本:
Aggregation agg = newAggregation(Class1.class,
match(criteria),
group("prop1", "prop2")
.avg("x").as("averageX")
);
我只需要知道如何在Spring mongo模板中获得此聚合结果的计数即可.
I just need to know how to get count of this aggregation result in spring mongo template.
推荐答案
我的回复来得很晚,但可能会对其他人有所帮助.要获得聚合计数,您需要在末尾添加一个新组:
My response comes very late but it might help others. To get the count for an aggregation you need to add a new group at the end:
在聚合末尾添加-> Aggregation.group().count().as("count")以获取计数
Add at the end of the aggregation -> Aggregation.group().count().as("count") to get the 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());
这篇关于如何在Spring Mongo模板中获取聚合查询计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!