我有一个集合,里面充满了这样的文档:
{
_id: ObjectId("51715ade030112703e4c1fd3"),
gas_price: 78,
elec_usage: 110,
elec_price: 88,
gas_usage: 146,
user: "test_user",
timestamp: ISODate("2013-04-19T15:55:26.813Z")
}我想向它们查询“时间戳就是今天”。我的查询JSON如下所示:
{"timestamp" : { $gte : new Date('2013-4-23')}}如果我手动运行这个查询,它会起作用:
find({"timestamp" : { $gte : new Date('2013-4-23')}}).limit(100)
[{ _id: ObjectId("51763f708b26fe92e77d7898"), gas_price: 98, elec_usage: 146, elec_price: 82, gas_usage: 120, user: "test_user", timestamp: ISODate("2013-04-23T08:59:44.587Z") }
{ _id: ObjectId("517646798b26fe92e77d7899"), gas_price: 76, elec_usage: 84, elec_price: 91, gas_usage: 117, user: "test_user", timestamp: ISODate("2013-04-23T09:29:45.693Z") }
{ _id: ObjectId("51764d858b26fe92e77d789a"), gas_price: 90, elec_usage: 69, elec_price: 87, gas_usage: 30, user: "test_user", timestamp: ISODate("2013-04-23T09:59:49.702Z") }
{ _id: ObjectId("517663058b26fe9629162cfe"), gas_price: 100, elec_usage: 38, elec_price: 80, gas_usage: 68, user: "test_user", timestamp: ISODate("2013-04-23T11:31:33.800Z") }
{ _id: ObjectId("517663868b26fe9637611ca4"), gas_price: 95, elec_usage: 147, elec_price: 93, gas_usage: 117, user: "test_user", timestamp: ISODate("2013-04-23T11:33:42.693Z") }]但是如果我在URL中进行查询:
https://api.mongohq.com/databases/vIHD_Development/collections/hourly_usage/documents?_apikey=xxxxxxxxxxxx&q=%7B%22timestamp%22%20:%20%7B%20$gte%20:%20new%20Date('2013-4-23')%7D%7D(未编码为"https://api.mongohq.com/databases/vIHD_Development/collections/hourly_usage/documents?_apikey=xxxxxxxxxxxx&q={"reading_time" : { $gte : new Date('2013-4-23')}}“)
我得到一个空数组。我尝试过不同的编码(这个网址包含$的%24 )和不同的结构(ISODate而不是日期,不同的日期格式),但似乎都不起作用。如果我执行一个更简单的查询{"user“:"test_user"},我确实会得到返回的文档,例如:
https://api.mongohq.com/databases/vIHD_Development/collections/hourly_usage/documents?_apikey=xxxxxxxxxxxx&q=%7Bgas_price:%20%7B%20$gte:%2070%7D%20%7D我最近更改了timestamp字段名称,而不是潜在冲突的timestamp,但对检索没有任何影响。
发布于 2014-02-03 21:44:58
似乎MongoHQ在REST API中对$date的实现并不完整。
我通过将日期存储为自纪元以来的时间戳秒数并对其进行查询来解决此问题。它不仅可以工作,而且代码也简单得多。
https://stackoverflow.com/questions/16173453
复制相似问题