简体   繁体   English

@Query [mongo/springboot] 中的日期比较有问题

[英]Having problem with date comparison in @Query [mongo/springboot]

I am trying to create a custom Query annotation in a MongoRepository.我正在尝试在 MongoRepository 中创建自定义查询注释。 Everything is fine except the date comparison.一切都很好,除了日期比较。 I need to find all items that were created on a specific day, so I give 2 date objects with the good date and the times fixed to 00:00 and 23:59 to gte and lte operators.我需要找到在特定日期创建的所有项目,所以我给 gte 和 lte 操作员提供了 2 个日期对象,它们的日期和时间固定为 00:00 和 23:59。 This is the request sent according to the debug trace:这是根据调试跟踪发送的请求:

2022-08-27 01:52:25.817 DEBUG 440959 --- [or-http-epoll-4] o.s.data.mongodb.core.MongoTemplate      : find using query: { "$and" : [{ "$or" : [{ "$where" : "5 == null"}, { "rating" : 5}]}, { "$or" : [{ "$where" : "null == null"}, { "productId" : null}]}, { "$or" : [{ "$where" : "APPROVEDD == null"}, { "moderationStatus" : "APPROVEDD"}]}, { "$or" : [{ "$where" : "true == false"}, { "clientResponses" : { "$exists" : true, "$not" : { "$size" : 0}}}]}, { "$or" : [{ "$where" : "2022-01-29T23:00:00Z == null || 2022-01-30T22:59:59Z == null"}, { "submissionTime" : { "$gte" : { "$date" : "2022-01-29T23:00:00Z"}, "$lte" : { "$date" : "2022-01-30T22:59:59Z"}}}]}]} fields: Document{{}} for class: class com.company.productreviews.cdpresponseportalapi.model.Review in collection: reviews

Dates seem to be in the good format, but mongo gives me an error (only when I use the date filter of my request):日期似乎格式不错,但 mongo 给了我一个错误(仅当我使用请求的日期过滤器时):

Query failed with error code 139 and error message 'SyntaxError: identifier starts immediately after numeric literal' on server mongodb-databaserevqa-shard-00-02.xxocp.mongodb.net:27017; nested exception is com.mongodb.MongoQueryException: Query failed with error code 139 and error message 'SyntaxError: identifier starts immediately after numeric literal' on server mongodb-databaserevqa-shard-00-02.xxocp.mongodb.net:27017

My repo:我的回购:

public interface ReviewRepository extends MongoRepository<Review, String> {
    @Query("{ $and : [" +
            "{ $or : [ { $where: '?0 == null' }, { 'rating': ?0 } ] }," +
            "{ $or : [ { $where: '?1 == null' }, { 'productId': ?1 } ] }," +
            "{ $or : [ { $where: '?2 == null' }, { 'moderationStatus': ?2 } ] }," +
            "{ $or : [ { $where: '?3 == false' }, { 'clientResponses': { $exists: true, $not: { $size: 0 } } } ] }," +
            "{ $or : [ { $where: '?4 == null || ?5 == null' }, { 'submissionTime': { $gte: ?4, $lte: ?5 } } ] }," +
            "]}")
    List<Review> findAll(Integer rating, Integer productId, String moderationStatus, boolean withAnswersOnly, Date submissionDateStartRange, Date submissionDateEndRange);

    Review findOneByReviewId(int reviewId);
}

It seems to be a syntax error when you date value are not null.当您的日期值不是 null 时,这似乎是一个语法错误。 Wrapping your statement in quote should do the trick.用引号包裹你的陈述应该可以解决问题。 So in your last @Query statement do the following:因此,在您的最后一个 @Query 语句中执行以下操作:

 "{ $or : [ { $where: '\"?4\" == \"null\" || \"?5\" == \"null\"' }, { 'submissionTime': { $gte: ?4, $lte: ?5 } } ] },"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM