使用 MongoDB Spring Data Aggregation 匹配日期時間字段的問題 (Problem with matching date-time fields using MongoDB Spring Data Aggregation)


問題描述

使用 MongoDB Spring Data Aggregation 匹配日期時間字段的問題 (Problem with matching date‑time fields using MongoDB Spring Data Aggregation)

只想在 MongoDB 中以長格式(日期以秒為單位)對日期進行 MatchOperation fromDate 和 toDate 變量的數據類型為 long。它除以 1000 以獲取秒格式的數據,而不是毫秒。

long fromDate = new Date().getTime()/1000;

日期時間字段格式在mongo文檔中

“datetime” : NumberInt(1595745447),

MatchOperation matchDate = Aggregation.match(Criteria.where("datetime")
                .gte(fromDate)
                .lte(toDate));

使用它形成的聚合是

db.some_collection.aggregate([{ "$match" : { "datetime" : { "$gte" : { "$numberLong" : "1595356200"}, "$lte" : { "$numberLong" : "1595375999"}}}}])

但它不起作用。它給出了 0 個結果。但是,當通過刪除

"$numberLong"

手動修改相同的內容時(如下所示)

它按預期提供結果和過濾器。

db.some_collection.aggregate([{ "$match" : { "datetime" : { "$gte" :1595356200, "$lte" : 1595375999}}}])

有人可以幫我解決這個問題嗎?提前致謝。


參考解法

方法 1:

There is a syntax error. NumberLong is a function not an operator.

Refer

Change

   { "$numberLong" : "1595356200"},

To

  NumberLong("1595356200")

(by Vipin MakdeGibbs)

參考文件

  1. Problem with matching date‑time fields using MongoDB Spring Data Aggregation (CC BY‑SA 2.5/3.0/4.0)

#spring-data-mongodb #mongoDB #aggregation-framework






相關問題

如何在 mongodb 中保存 java.sql.date 對象? (How to save java.sql.date object in mongodb?)

Spring Data MongoDB Core 1.9.1.RELEASE 給出 java.lang.NoClassDefFoundError: org/springframework/data/geo/GeoResults (Spring Data MongoDB Core 1.9.1.RELEASE gives java.lang.NoClassDefFoundError: org/springframework/data/geo/GeoResults)

我可以結合 AND 和 OR 條件嗎 (Can I combine AND and OR conditions)

查詢數據時應該使用 MongoTemplate 還是 DBCollection (Should i use MongoTemplate or DBCollection when query data)

是否有可能使用 Spring Data MongoDb 來定義自定義標準? (Is possibility using Spring Data MongoDb to define custom Criteria?)

Spring-data 2.1 使用 kotlin 獲取“UnsupportedOperationException:沒有訪問器設置屬性” (Spring-data 2.1 get "UnsupportedOperationException: No accessor to set property" with kotlin)

調用頁面大小大於 36 的 Spring Data MongoDB 存儲庫方法時出現 StackOverflowError (StackOverflowError when calling Spring Data MongoDB repository method with a page size bigger than 36)

JSON- MongoDB 中帶有 Spring Data 的數組 (JSON- Array in MongoDB with Spring Data)

Spring Data Mongo DB:回复消息長度5502322小於最大消息長度 (Spring Data Mongo DB: The reply message length 5502322 is less than the maximum message length)

Spring Boot 反應式和 mongodb '命令插入需要身份驗證' (Spring boot reactive and mongodb 'command insert requires authentication')

使用 MongoDB Spring Data Aggregation 匹配日期時間字段的問題 (Problem with matching date-time fields using MongoDB Spring Data Aggregation)

在 Spring Boot 應用程序中實現工作進程 (Implementing worker processes in a Spring Boot application)







留言討論