MongoHQ Rest API日期查询

时间:2013-04-23 15:32:56

标签: rest date mongohq

我有一个包含这样的文档的集合:

{
  _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")
}

我想查询它们“timestamp is today”。我的查询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") }]

但如果我在网址中进行查询:

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')}}”)

我得到一个空数组。我尝试过不同的编码(这个URL包含$ 24的$)和不同的结构(ISODate而不是Date,不同的日期格式)但似乎没有任何效果。如果我做一个更简单的查询{“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

我最近更改了时间戳字段名称,使其远离可能存在冲突的时间戳,而不会对检索产生任何影响。

1 个答案:

答案 0 :(得分:0)

似乎MongoHQ在他们的REST API中实现$ date并不完整。

我通过将日期存储为自纪元以来的时间戳秒并对其进行查询来解决此问题。它不仅有效,而且代码更简单。