英文:
Error: Cannot resolve symbol 'DateOperators' - MongoTemplate
问题
我想将以下代码添加到我的项目中:
Aggregation aggregation = newAggregation(
project(from(field("dayActivity.type", "dayActivity.type"),
field("tid"))).
and(DateOperators.dateOf("activityDate").toString("%Y-%m-%d")).as("yearMonthDay"),
match(where("yearMonthDay").is("2017-11-05").and("tid").is("12345678"))
);
我甚至无法在项目中添加DateOperators.class或特定的Maven依赖项。在导入时显示错误:
import org.springframework.data.mongodb.core.aggregation.DateOperators;
错误:无法解析符号'DateOperators'
-- 使用4.0.5版本的MongoDB。
我应该如何继续?
英文:
I want to add the below code to my project.
Aggregation aggregation = newAggregation(
project(from(field("dayActivity.type", "dayActivity.type"),
field("tid"))).
and(DateOperators.dateOf("activityDate").toString("%Y-%m-%d")).as("yearMonthDay"),
match(where("yearMonthDay").is("2017-11-05").and("tid").is("12345678"))
);
I am not even able to add DateOperators.class or the particular maven dependency in my project.
It is showing an error
while importing :
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"
的字符串,使用以下语法:
project()
.and(DateOperators.DateToString
.dateOf("activityDate")
.toString("%Y-%m-%d"))
.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:
project()
.and(DateOperators.DateToString
.dateOf("activityDate")
.toString("%Y-%m-%d"))
.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"
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论