問題描述
使用 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.
Change
{ "$numberLong" : "1595356200"},
To
NumberLong("1595356200")
(by Vipin Makde、Gibbs)