есть коллекция из этого надо сгруппировать документы по часам в монго запрос выглядит следующим образом:
db.user_action.aggregate([
// Get only records created in a time span
{$match: {createdDate: {$gte: ISODate("2022-01-01T00:00:00Z"), $lte: ISODate("2022-01-10T00:00:00Z")}}},
// Get the year, month and day from the createdTimeStamp
{
$project: {
"year": {$year: "$createdDate"},
"month": {$month: "$createdDate"},
"day": {$dayOfMonth: "$createdDate"},
"hour": {$hour: "$createdDate"}
}
},
// Group by year, month, day and hour and get the count
{
$group: {
_id: {
year: "$year",
month: "$month",
day: "$day",
hour: "$hour"
},
"count": {
$sum: 1
}
}
}
])
запрос работает корректно но как писать такой запрос в java? пока делал так но работает не корректно
MatchOperation filterStates = match(new Criteria("createdDate")
.gte(t1).lte(t2));
ProjectionOperation projectionOperation = project("createdDate", "year").and("createdDate").as("year")
.andExpression("createdDate").as("year")
.andExpression("createdDate").as("month")
.andExpression("createdDate").as("day")
.andExpression("createdDate").as("hour");
GroupOperation groupByUserAndSumUserId = Aggregation.group("year", "month", "day", "hour").count().as("sum");
Aggregation aggregation
= newAggregation(filterStates, projectionOperation, groupByUserAndSumUserId);
return reactiveMongoTemplate.aggregate(aggregation, "user_action", String.class);