Unsupported query by ISODate

207 views
Skip to first unread message

Pau Freixes

unread,
Mar 25, 2014, 1:30:04 PM3/25/14
to pytho...@googlegroups.com
Hi mates,

I've been looking for current support on Eve for query collections using mongo db where syntax. 

It seemed to be fine until I found a couple of problems using queries with dates and ids. I'm gonna focus with dates but it is the same for ObjectIds

The next programatic way to run a query by date looks to be wrong:

>>> q = """{"_created":{"$gte":"2014-12-01"}}"""
>>> r = requests.get("http://localhost:5000/resource/", params={"where": q})
>>> len(r.json()["_items"]
0

Actually, when I tried to make a request using a ISODate it returns a 400 code error:

>>> import requests
>>> q = """{"_created":{"$gte":ISODate("2014-12-01")}}"""
>>> r = requests.get("http://localhost:5000/resource/", params={"where": q})
>>> r
<Response [400]>

It looks to be right, current ISODate is not a right valid field type/syntax for default json module.

>>> json.loads("""{"_created":{"$gte":ISODate("0")}}""")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

But defintly it can be executed under mongo console:

$ mongo
> use mydb
> db.resource.find({"_created": {"$gte":ISODate("2014-12-01")}})
> .....lots of results 

It looks to come from how Eve is dealing with json serialization. 

Eve uses pymongo when _created and _updated field are created and these fields have to be encoded, but It uses default Json module to encode where parameters, and it has no support for these kind of fields.

Also Eve uses flask jsonify function for encode, and this encode has encoder to give support for date time objects.

Anyone else has encountered these problems? Any ideas to deal with this ?

Thanks.







--
--pau

Javier Gonel

unread,
Mar 29, 2014, 6:44:37 AM3/29/14
to pytho...@googlegroups.com
Hello Pau,


On Tuesday, March 25, 2014 7:30:04 PM UTC+2, Pau Freixes wrote:
Hi mates,

I've been looking for current support on Eve for query collections using mongo db where syntax. 

It seemed to be fine until I found a couple of problems using queries with dates and ids. I'm gonna focus with dates but it is the same for ObjectIds

The next programatic way to run a query by date looks to be wrong:

>>> q = """{"_created":{"$gte":"2014-12-01"}}"""
>>> r = requests.get("http://localhost:5000/resource/", params={"where": q})
>>> len(r.json()["_items"]
0

AFAIK the way eve handles dates is using the DATE_FORMAT configuration setting:

So for a value to be understood as a date it has to be in the format specified in DATE_FORMAT. For example, in the demo-eve application:
curl --data-urlencode 'where={"_created":{"$lte": "Sat, 29 Mar 2014 12:38:13 GMT"}}' -G -v http://eve-demo.herokuapp.com/people/

The specific code doing the job:

I hope it helps :)

Nicola Iarocci

unread,
Apr 1, 2014, 3:17:28 AM4/1/14
to pytho...@googlegroups.com
Yes I find that implicit handling of date and objectid fields is easier on the eyes :) Consider that not all API end users are going to be familiar with MongoDB internals. Also, this way both supported syntaxes (Mongo and Python) are aligned when it comes to dates and objectid types. 

You are correct that I should update the docs with some specific examples. Thanks!

Cheers,
Nicola

vishwa...@gmail.com

unread,
Aug 4, 2018, 2:37:53 PM8/4/18
to Eve REST Framework
Did we move further on this ? The thing I am working on also invalidating json due to ISODate ?
Reply all
Reply to author
Forward
0 new messages