1

есть коллекция из этого надо сгруппировать документы по часам в монго запрос выглядит следующим образом:

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);

1 ответ 1

1

как минимум нашел два варианта: первый вариант:

MatchOperation filterStates = match(new Criteria("createdDate")
        .gte(t1).lte(t2));

ProjectionOperation projectionOperation = project()
        .and(DateOperators.Year.yearOf("createdDate")).as("year")
        .and(DateOperators.Month.monthOf("createdDate")).as("month")
        .and(DateOperators.DayOfMonth.dayOfMonth("createdDate")).as("day")
        .and(DateOperators.Hour.hourOf("createdDate")).as("hour");

GroupOperation groupByUserAndSumUserId = Aggregation.group("year", "month", "day", "hour").count().as("count");

Aggregation aggregation
        = newAggregation(filterStates, projectionOperation, groupByUserAndSumUserId);

return reactiveMongoTemplate.aggregate(aggregation, "user_action", UserActionPerHour.class);

и второй вариант:

MatchOperation filterStates = match(new Criteria("createdDate")
                .gte(t1).lte(t2));

        ProjectionOperation projectionOperation = project()
                .and("createdDate").dateAsFormattedString("%Y-%m-%d %H").as("yearMonthDayUTC");

        GroupOperation groupByUserAndSumUserId = Aggregation.group("yearMonthDayUTC").count().as("count");

        Aggregation aggregation
                = newAggregation(filterStates, projectionOperation, groupByUserAndSumUserId);

        return reactiveMongoTemplate.aggregate(aggregation, "user_action", UserActionPerHour.class);

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.