Can not filter using a string field using python-eve when the value is the _id for other object in mongodb

StackOverflow https://stackoverflow.com//questions/21011247

  •  21-12-2019
  •  | 
  •  

Question

I'm experiencing a weird behavior when trying to filter a collection using python-eve, when I filter using the "where" parameter a field that contains another document's _id as a value, no record is returned, if I add an extra letter/number after the field value the record is returned with no issues, the same behavior happens with any field having an _id as value. As I see in the profile looks like the value is transformed into an $oid when calling the db, but I'm not sure why Any idea?

Thanks Gaston

URL

http://127.0.0.1:5000/contacts?where={"task":"52cdad82a54d7506bc6f9a12"}

Returned Data

<resource href="/contacts" title="contacts">
    <link rel="parent" href="" title="home"/>
</resource>

Settings.py

contacts= {
    'resource_methods': ['GET', 'POST'],
     'schema':  {

             "timestamp":  {
             'type': 'datetime',
         },
            "task":  {
                'type': 'string'
            },
                "contact_uuid":  {
                'type': 'string'
            },
                "started_by":  {
                'type': 'string'
            },
        }
}

MongoDB Record

{
"_id": {
    "$oid": "52cdc33ca54d75085073e7ef"
},
"timestamp": {
    "$date": "2014-01-08T21:29:32.603Z"
},
"task": "52cdad82a54d7506bc6f9a12",
"contact_uuid": "43acc81a-bdd2-42dd-bc0f-8279ddd497f4",
"started_by": "admin"
}

MongoDB Profile Output

{
"op": "query",
 "ns": "callc.contacts",
 "query": {
"task": {
  "$oid": "52cdad82a54d7506bc6f9a16"
    }
  },
"ntoreturn": 10,
"ntoskip": 0,
"nscanned": 9,
"keyUpdates": 0,
"numYield": 0,
  "lockStats": {
"timeLockedMicros": {
      "r": 172,
  "w": 0
    },
"timeAcquiringMicros": {
  "r": 4,
  "w": 4
}
  },
  "nreturned": 0,
  "responseLength": 20,
  "millis": 0,
  "ts": {
    "$date": "2014-01-09T04:04:31.578Z"
  },
  "client": "XXX.17.153.XXX",
  "allUsers": [
    {
      "user": "xxxxx",
      "userSource": "callc"
    }
  ],
      "user": "xxxx@callc"
}

I'm using a db hosted in mongolab running mongod 2.4.7

And virtualenv, with the following "pip freeze" output

  • Cerberus==0.4.0
  • Eve==0.2
  • Events==0.2.0
  • Flask==0.10.1
  • Flask-Principal==0.4.0
  • Flask-PyMongo==0.3.0
  • Flask-Uploads==0.1.3
  • Flask-WTF==0.9.3
  • Jinja2==2.7
  • MarkupSafe==0.18
  • WTForms==1.0.5
  • Werkzeug==0.9.4
  • blinker==1.3
  • flask-mongoengine==0.7.0
  • itsdangerous==0.22
  • mongoengine==0.8.6
  • passlib==1.6.1
  • py==1.4.18
  • pymongo==2.6.3
  • pytest==2.4.2
  • requests==2.1.0
  • simplejson==3.3.0
  • wsgiref==0.1.2
Was it helpful?

Solution

The latest dev version supports query_objectid_as_string. Set it to True for your "task" field and you should be fine. See docs for details.

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