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

124 views Asked by At

`

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??

I just want to count all occurences after group by . Basically similar to select count(*) from table group by name;

1

There are 1 answers

0
Kassutta Fit On

UPDATE: Got through the docs after investing hours. We can use sum(literal(1)) to do the same in aggregation query in kotlin.