zho*_*zhe 5 mongodb spring-mongodb
我正在使用 spring mongo 模板在 mongodb 上运行协议查询。我想知道有没有办法找出spring mongo模板中聚合结果的数量?
这是我的聚合示例:
Aggregation agg = newAggregation(Class1.class,
match(criteria),
group("prop1", "prop2")
.avg("x").as("averageX")
);
Run Code Online (Sandbox Code Playgroud)
我只需要知道如何在 spring mongo 模板中计算这个聚合结果。
我的回复来得很晚,但它可能会帮助其他人。要获得聚合的计数,您需要在最后添加一个新组:
在聚合末尾添加 -> 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")
);
Run Code Online (Sandbox Code Playgroud)
要获得计数:
Long.parseLong(results.getMappedResults().get(0).get("count").toString());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8734 次 |
| 最近记录: |