简体   繁体   English

如何使用 datastore.aggregate() 在最新的 morphia 版本 2.3 中计算 groupby 中的 count(*)?

[英]How to calculate count(*) in groupby in latest morphia version 2.3 using datastore.aggregate()?

` `

datastore.aggregate()
            .match()
            .project()
            .unwind()
            .group(group(id("my_id")).field("totalCount", sum("1")).field("average", avg(field("field2"))))
            .execute(result::class.java)
            .toList()

` `

In CLI it works something like $sum: 1 in group to do a count(*) but how to do this in kotlin/java code??在 CLI 中,它的工作原理类似于 $sum: 1 in group 来执行计数(*) 但如何在 kotlin/java 代码中执行此操作?

I just want to count all occurences after group by.我只想计算分组后的所有事件。 Basically similar to select count(*) from table group by name;基本上类似于select count(*) from table group by name;

UPDATE: Got through the docs after investing hours.更新:投入数小时后完成了文档。 We can use sum(literal(1)) to do the same in aggregation query in kotlin.我们可以使用sum(literal(1))在 kotlin 的聚合查询中做同样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM