本文介绍了MongoRepository @Query无法将字符串解析为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我的问题是通过Spring JPA(MongoRepository)在MongoDB中搜索集合.

First of all, my issue is searching Collecttions in MongoDB via Spring JPA (MongoRepository).

我的对象:

{
    "_id" : ObjectId("5c78e1f447f39c2eacb229d7"),
    "lab" : "xxx",
    "type" : "Holiday",
    "description" : "Lunar New Year",
    "start_date" : ISODate("2019-02-04T02:37:42.152Z"),
    "end_date" : ISODate("2019-02-08T06:37:42.152Z"),
    "all_day" : true,
    "_class" : "xxx.Event"
}

我可以在Mongo查询中按我的意愿做

i can do as my wish in Mongo query as:

db.getCollection('event').find({"start_date" : {$gte :ISODate( "2019-02-03T02:37:42.152Z") , $lte :ISODate( "2019-02-08T02:37:42.152Z")}})

(您可以将ISODate替换为新的日期)

(you can replace ISODate with new Date)

但是要在春季做到这一点,我想这样做:

But to do it in Spring, i want to do it as:

@Query("   $or: [ {start_date : {$gte :ISODate( ?0 ) , $lte :ISODate( ?1)}} , {end_date : {$gte :ISODate( ?0) , $lte :ISODate( ?1)}} ]  }  ")
List<Event> findAllEventByTime(String from, String to);

但是失败了,我搜索了两个主题:此处

But it fail, i searched in two topic:hereand there

并以

@Query("{ 'start_date' : {$gte : {'$date': '?0'}, $lte :{'$date': '?1'} }}")
List<Event> findAllEventByTime(String from, String to);

但是再次,我在解析时遇到了问题:

But once again, i had the problem with parsing:

org.bson.json.JsonParseException:无法将字符串作为日期解析为 org.bson.json.JsonReader.visitDateTimeExtendedJson(JsonReader.java:1057)

org.bson.json.JsonParseException: Failed to parse string as a date at org.bson.json.JsonReader.visitDateTimeExtendedJson(JsonReader.java:1057)

我尝试以下建议:

尝试参数:3月22日星期五10:09:48 ICT 2019和2019-03-22T03:09:48.227Z和2016-04-14 00:00:00

Try param: Fri Mar 22 10:09:48 ICT 2019 and 2019-03-22T03:09:48.227Z and 2016-04-14 00:00:00

所有这些都消失了...你们可以帮我解决这个问题吗?

All of this going down...Can you guys help me to fix it?

工作流:来自FE(字符串)的参数〜>转到BE〜>如上所述调用回购

Work-Flow: Params from FE (String) ~> Go to BE ~> Call Repo as above

推荐答案

我用其他方法解决该问题:

I resolve it with other method:

  1. 在存储库中
    • 我无法使用原始查询或任何需要从String转换为Date的查询进行转换
    • 使用MongoRepository的自动生成查询支持
  1. In repository
    • I can't convert with raw query or any query that need to convert from String to Date
    • Use the auto generate query support by MongoRepository
Page<Event> findAllByStartDateBetweenOrEndDateBetween(Instant fromDate1, Instant toDate1, Instant fromDate2, Instant toDate2, Pageable pageable);
    List<Event> findAllByStartDateBetweenOrEndDateBetween(Instant fromDate1, Instant toDate1, Instant fromDate2, Instant toDate2);
  1. 输入数据:使用Instant或LocalDate/LocalDateTime并将其转换为Instant〜>然后在查询中用作参数(由Spring自动转换)
  1. Input Data:Use Instant or LocalDate / LocalDateTime and convert it to Instant~> Then use as param in query (auto convert by Spring)
@RequestParam Instant startDate, @RequestParam Instant endDate

并使用:

eventRepository.findAllByStartDateBetweenOrEndDateBetween(startDate, endDate, startDate, endDate))

这篇关于MongoRepository @Query无法将字符串解析为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 21:50
查看更多