Spring Data mongodb: «год» должен быть целым числом - PullRequest
0 голосов
/ 21 декабря 2018

Я построил это агрегирование:

ProjectionOperation projectStage = Aggregation
    .project("application", "uploadedRefs", "uploadedKb", "downloadedDocs", "downloadedKb")
        .and(DateOperators.Year.yearOf("timestamp")).as("year")
        .and(DateOperators.Month.monthOf("timestamp")).as("month")
        .and(DateOperators.DayOfMonth.dayOfMonth("timestamp")).as("day")
        .and(DateOperators.DateFromParts.dateFromParts()
            .yearOf("timestamp")
            .monthOf("timestamp")
            .dayOf("timestamp")
        ).as("startIntervalTimestamp");

Aggregation aggregation = Aggregation
        .newAggregation(
            projectStage
        );

System.out.println(aggregation.toString());

Вывод:

[
   {
      "$project":{
         "application":1,
         "uploadedRefs":1,
         "uploadedKb":1,
         "downloadedDocs":1,
         "downloadedKb":1,
         "year":{
            "$year":"$timestamp"
         },
         "month":{
            "$month":"$timestamp"
         },
         "day":{
            "$dayOfMonth":"$timestamp"
         },
         "startIntervalTimestamp":{
            "$dateFromParts":{
               "year":"timestamp",
               "month":"timestamp",
               "day":"timestamp"
            }
         }
      }
   }
]

Сообщение об ошибке:

Error: command failed: {
    "ok" : 0,
    "errmsg" : "'year' must evaluate to an integer, found string with value \"timestamp\"",
    "code" : 40515,
    "codeName" : "Location40515"
} 

1 Ответ

0 голосов
/ 21 декабря 2018

Решено:

Field timestampField = Fields.field("timestamp");
ProjectionOperation projectStage = Aggregation
    .project("application", "uploadedRefs", "uploadedKb", "downloadedDocs", "downloadedKb")
        .and(DateOperators.Year.year(timestampField)).as("year")
        .and(DateOperators.Month.month(timestampField)).as("month")
        .and(DateOperators.DayOfMonth.dayOfMonth(timestampField)).as("day")
        .and(DateOperators.DateFromParts.dateFromParts()
            .year(DateOperators.Year.year(timestampField))
            .month(DateOperators.Month.month(timestampField))
            .day(DateOperators.DayOfMonth.dayOfMonth(timestampField))
        ).as("startIntervalTimestamp");
...