错误:无法解析符号’DateOperators’ – MongoTemplate

huangapple go评论148阅读模式
英文:

Error: Cannot resolve symbol 'DateOperators' - MongoTemplate

问题

我想将以下代码添加到我的项目中:

  1. Aggregation aggregation = newAggregation(
  2. project(from(field("dayActivity.type", "dayActivity.type"),
  3. field("tid"))).
  4. and(DateOperators.dateOf("activityDate").toString("%Y-%m-%d")).as("yearMonthDay"),
  5. match(where("yearMonthDay").is("2017-11-05").and("tid").is("12345678"))
  6. );

我甚至无法在项目中添加DateOperators.class或特定的Maven依赖项。在导入时显示错误:

  1. import org.springframework.data.mongodb.core.aggregation.DateOperators;

错误:无法解析符号'DateOperators'

-- 使用4.0.5版本的MongoDB。

我应该如何继续?

英文:

I want to add the below code to my project.

  1. Aggregation aggregation = newAggregation(
  2. project(from(field("dayActivity.type", "dayActivity.type"),
  3. field("tid"))).
  4. and(DateOperators.dateOf("activityDate").toString("%Y-%m-%d")).as("yearMonthDay"),
  5. match(where("yearMonthDay").is("2017-11-05").and("tid").is("12345678"))
  6. );

I am not even able to add DateOperators.class or the particular maven dependency in my project.
It is showing an error
while importing :

  1. import org.springframework.data.mongodb.core.aggregation.DateOperators;

Error: Cannot resolve symbol 'DateOperators'

--using a 4.0.5 MongoDB version.

How should I proceed?

答案1

得分: 0

要将date字段投影为格式为"%Y-%m-%d"的字符串,使用以下语法:

  1. project()
  2. .and(DateOperators.DateToString
  3. .dateOf("activityDate")
  4. .toString("%Y-%m-%d"))
  5. .as("yearMonthDay")

同时,你需要导入org.springframework.data.mongodb.core.aggregation.DateOperators。这在Spring Data MongoDB 2.3和MongoDB Server v4.2中可以正常工作。

例如,具有值ISODate("2020-09-23T12:10:15.710Z")的日期字段activityDate将被投影为具有名称yearMonthDay且值为"2020-09-23"的字段。

英文:

To project a date field as string of format "%Y-%m-%d"use this syntax:

  1. project()
  2. .and(DateOperators.DateToString
  3. .dateOf("activityDate")
  4. .toString("%Y-%m-%d"))
  5. .as("yearMonthDay")

And, you are looking to import org.springframework.data.mongodb.core.aggregation.DateOperators. This worked fine with Spring Data MongoDB 2.3 and MongoDB Server v4.2.

For example, the date field activityDate with value ISODate("2020-09-23T12:10:15.710Z") is projected as a field with name yearMonthDay with value "2020-09-23".

huangapple
  • 本文由 发表于 2020年10月23日 19:10:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64498883.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定