Question

I have a collection that is full of documents like this:

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

I want to query them for "timestamp is today". My query JSON looks like this:

{"timestamp" : { $gte : new Date('2013-4-23')}}

if I run this query manually it works:

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") }]

but if I make the query in a 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

(which unencoded is "https://api.mongohq.com/databases/vIHD_Development/collections/hourly_usage/documents?_apikey=xxxxxxxxxxxx&q={"reading_time" : { $gte : new Date('2013-4-23')}}" )

I get an empty array back. I've tried different encodings (this URL includes a %24 for $) and different constructions (ISODate rather than Date, different date formatting) but nothing seems to work. If I make a simpler query {"user" : "test_user"} I do get documents back e.g.:

https://api.mongohq.com/databases/vIHD_Development/collections/hourly_usage/documents?_apikey=xxxxxxxxxxxx&q=%7Bgas_price:%20%7B%20$gte:%2070%7D%20%7D

I have recently changed the timestamp field name away from the potentially clashing timestamp without any effect to the retrieval.

Was it helpful?

Solution

Seems that MongoHQ's implementation of $date in their REST API was not complete.

I resolved this by storing dates as timestamp seconds since the epoch and making my queries on that. Not only does it work, the code is much simpler.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top