The HOUR_COUNTS collections contains {docId, hour, count}
It's very easy for me to get the sum of the count of docId by using the following mongodb query:
db.HOUR_COUNTS.aggregate(
[
{
$match: { hour: { $gte: 10 } }
},
{
$group: { _id: "$docId", total: { $sum: "$count" } }
},
{
$sort: { total: -1, _id: -1 }
},
{
$limit: 20
}
]
)
Then I can get the following result:
{ "_id" : 6831, "total" : 6 }
{ "_id" : 6830, "total" : 6 }
{ "_id" : 6849, "total" : 4 }
{ "_id" : 6848, "total" : 4 }
{ "_id" : 6847, "total" : 3 }
It's the time for me to do it using Spring Data
I tried to do this but it is not going to work:
Aggregation agg = newAggregation(
match(where("hour").gte(0)),
project("docId"),
group("docId").sum("count").as("total"),
project("total").and("docId").previousOperation(),
sort(Sort.Direction.DESC, "total", "docId"),
limit(20)
);
The error is:
java.lang.IllegalArgumentException: Invalid reference 'count'!
Therefore, I would like to know how to make the query work on Spring Data. Thank you.