簡體   English   中英

@Query [mongo/springboot] 中的日期比較有問題

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

我正在嘗試在 MongoRepository 中創建自定義查詢注釋。 一切都很好,除了日期比較。 我需要找到在特定日期創建的所有項目,所以我給 gte 和 lte 操作員提供了 2 個日期對象,它們的日期和時間固定為 00:00 和 23:59。 這是根據調試跟蹤發送的請求:

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

日期似乎格式不錯,但 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

我的回購:

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);
}

當您的日期值不是 null 時,這似乎是一個語法錯誤。 用引號包裹你的陳述應該可以解決問題。 因此,在您的最后一個 @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