SoftInstigate / restheart

Rapid API Development with MongoDB
https://restheart.org
GNU Affero General Public License v3.0
805 stars 171 forks source link

Last 15 minutes document query #142

Closed AntonioSpalluto closed 8 years ago

AntonioSpalluto commented 8 years ago

Hi all,

I tried to use a mongodb syntax into the filter parameter to get the last created documents in the previous 15 minutes. The querystring parameter looks like this:

?filter={"createdAt":{"$gte":"new Date(ISODate().getTime()-1000*60*15)"}}

This works on mongo DB shell but not in the querystring. Is this supported by the current version?

Thanks

ujibang commented 8 years ago

yes it is supported.

restheart uses the so called Strict mode representations of BSON, your filter uses the mongo Shell mode.

// using  ISO-8601 date format following the template YYYY-MM-DDTHH:mm:ss.mmm<+/-Offset>.

?filter={"createdAt":{"$gte": {"$date": "2016-07-06T05:00:00.000"}}}

// or using unix epoch time milliseconds

?filter={"createdAt":{"$gte": {"$date": 1467792899115}}}

more info

AntonioSpalluto commented 8 years ago

Hi Andrea,

This doesn't apply to my question. The matter is I need to get the timestamp from the server and not calculate it on client side.

Thanks

ujibang commented 8 years ago

Hi @AntonioSpalluto

for server side date, you can define an aggregation pipeline, mapReduce or simply use the $where operator:

example: $where operator with httpie:

http GET 127.0.0.1:8080/test/coll?filter='{"$where": "this.date > new Date(ISODate().getTime() - 1000*60*15)"}'
AntonioSpalluto commented 8 years ago

Hi Andrea,

Thanks for the hint, I will try the pipeline and the mapReduce solutions. The $where has very poor perfomance and I cannot use it.

I'll let you know the final solution.

Thanks

ujibang commented 8 years ago

Hi @AntonioSpalluto

if you are familiar with java, you can also have a look at the restheart custom application logic

I think that this would be the best solution having the (current date - 15 minutes) computed server side by restheart

AntonioSpalluto commented 8 years ago

Hi @ujibang,

I just check it out and it looks quite simple starting from the examples: good job!

Thanks Antonio

ujibang commented 8 years ago

Hi @AntonioSpalluto

closing this question now, feel free to reopen if furhter information is needed