gpt4 book ai didi

java - mongodb 按查询范围过滤的问题

转载 作者:行者123 更新时间:2023-12-02 10:08:53 25 4
gpt4 key购买 nike

我遇到了 mongodb 问题。谁能解释一下为什么这有效(返回一些结果):

db.getCollection('test').find({"reportedOn" : { "$gte" :    ISODate("2019-03-01T01:20:00"), "$lt" : ISODate("2019-04-01T02:00:00") } });

这不起作用(返回零):

db.getCollection('test').find({"reportedOn" : { "$gte" : { "$date" : 1551398400000 }, "$lt" : { "$date" : 1554073200000 } } });

非常感谢。

最佳答案

您可能需要检查此类related topic了解为什么您实际上不能直接在查询中使用纪元时间。

这是查询日期的有趣部分:

根据Data Types in the mongo Shell两者应该是等效的:

The mongo db provides various methods to return the date, either as a string or as a Date object:

  • Date() method which returns the current date as a string.
  • new Date() constructor which returns a Date object using the ISODate() wrapper.
  • ISODate() constructor which returns a Date object using the ISODate() wrapper.

并使用ISODate should still return a Date object .

{"$date": "ISO-8601 string"} 可以在需要严格的 JSON 表示时使用。

所以你可能想检查all ISO-8601 available formats并根据预期格式更新您的代码,以便使其按预期工作。

关于java - mongodb 按查询范围过滤的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55139738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com