使用spring mongo数据中mongodb中存储为字符串的日期进行查询
以下是我的密码-使用spring mongo数据中mongodb中存储为字符串的日期进行查询,mongodb,spring-boot,spring-mongodb,Mongodb,Spring Boot,Spring Mongodb,以下是我的密码- MatchOperation match_Status_Count = new MatchOperation( Criteria.where("date1").gte("2019-3-25T17:34:24.734Z").andOperator( Criteria.where("date1").lte("2019-11-25T17:34:24.734Z")
MatchOperation match_Status_Count = new MatchOperation(
Criteria.where("date1").gte("2019-3-25T17:34:24.734Z").andOperator(
Criteria.where("date1").lte("2019-11-25T17:34:24.734Z")
));
GroupOperation group_Status_Count = Aggregation.group("Status").count().as("Statuscount");
Aggregation aggregate_Status_Count = Aggregation
.newAggregation(match_Status_Count, group_Status_Count)
.withOptions(new AggregationOptions(false, false,
new BasicDBObject(new Document().append("batchSize", 100000000000L))));
;
AggregationResults<ARStructData> orderAggregate_Status_Count = mongoTemplate
.aggregate(aggregate_Status_Count, D_AR_COLLECTION_NAME, ARStructData.class);
MatchOperation match\u Status\u Count=新的匹配操作(
标准。其中(“日期1”)。gte(“2019-3-25T17:34:24.734Z”)。和操作员(
标准。其中(“日期1”).lte(“2019-11-25T17:34:24.734Z”)
));
GroupOperation group_Status_Count=Aggregation.group(“Status”).Count().as(“Statuscount”);
聚合聚合\状态\计数=聚合
.newAggregation(匹配状态计数、组状态计数)
.withOptions(新聚合选项)(false、false、,
新的BasicDBObject(新文档().append(“batchSize”,1000000000L));
;
AggregationResults orderAggregate\u Status\u Count=mongoTemplate
.aggregate(聚合状态、计数、D\U AR\U集合名称、ARStructData.class);
由于日期存储为字符串,因此它根据字符串gte和lte返回。
如果有人能为我提供解决方案,我将不胜感激。假设您的输入日期是字符串,您将希望将其转换为
java.util.Date
例如
Date lowerDate = yourFavoriteCvtDateStringtoDate(lowerDateString);
Date upperDate = yourFavoriteCvtDateStringtoDate(upperDateString);
然后创建一个查询,该查询利用$toDate
函数将文档中的日期字符串转换为可比较的真实日期(此处使用Javascript以便于查看):
所以你想说“给我两个日期之间的文档,其中输入日期是实际日期对象,但文档中的日期是字符串”?现在输入日期不是实际日期对象,而是字符串。但是修改它也不起作用。是的,文档中的日期是一个字符串
db.foo.aggregate([
{$addFields: {X: {$toDate: "$date1"} }}
,{$match: {$and: [ {"X": {$gt: lowerDate}}, {"X": {$lt: upperDate}} ] }}
]);