当值为 mongodb 中其他对象的 _id 时,无法使用 python-eve 过滤字符串字段

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

  •  21-12-2019
  •  | 
  •  

当我尝试使用 python-eve 过滤集合时,我遇到了奇怪的行为,当我使用“where”参数过滤包含另一个文档的 _id 作为值的字段时,如果我添加额外的字母/,则不会返回任何记录/字段值后的数字将毫无问题地返回记录,任何具有 _id 作为值的字段都会发生相同的行为。正如我在配置文件中看到的那样,在调用数据库时,该值似乎已转换为$ oid,但我不确定为什么知道吗?

谢谢加斯顿

网址

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

返回数据

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

设置.py

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

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

MongoDB 记录

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

MongoDB 配置文件输出

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

我正在使用 mongolab 托管的数据库,运行 mongod 2.4.7

和 virtualenv,具有以下“pip freeze”输出

  • 地狱犬==0.4.0
  • 夏娃==0.2
  • 事件==0.2.0
  • 烧瓶==0.10.1
  • Flask-Principal==0.4.0
  • Flask-PyMongo==0.3.0
  • Flask-上传==0.1.3
  • Flask-WTF==0.9.3
  • 金贾2==2.7
  • 标记安全==0.18
  • WTForms==1.0.5
  • 武器==0.9.4
  • 方向灯==1.3
  • 烧瓶-mongoengine==0.7.0
  • 危险==0.22
  • 蒙戈引擎==0.8.6
  • passlib==1.6.1
  • py==1.4.18
  • pymongo==2.6.3
  • pytest==2.4.2
  • 请求==2.1.0
  • simplejson==3.3.0
  • wsgiref==0.1.2
有帮助吗?

解决方案

最新开发版本支持 query_objectid_as_string. 。将其设置为 True 对于您的“任务”字段,您应该没问题。看 文档 了解详情。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top