我正在使用默认设置的 python-eve
'MONGO_QUERY_BLACKLIST': ['$where', '$regex']
但看来我仍然可以在对 Eve 的查询中使用“where”参数。
import requests
params = {'where': '{"username":"Alex"}'}
response = requests.get('http://localhost/users', params)
print response.content
print response.status_code
{"_items": [{"username": "Alex", ... }], ...}
200
请您参考如下方法:
您将 Eve 的名为 where 的 REST API 参数(将给定参数转换为标准 MongoDB find() 查询的查询条件)与 MongoDB 的 $where 混为一谈。 JavaScript 运算符(在 Eve 中,强烈不鼓励并默认禁用其使用)。
不幸的是,这是 Eve API 中令人困惑的命名选择。 $where 运算符(如果使用)将成为提供给 Eve 的 where 的查询条件的一部分。
修改您的示例参数以使用 $where 查询(仅用于说明目的,因为这绝对不值得推荐或性能不佳):
params = {'where': '{"$where":"this.username == \'Alex\'"}'}
使用 Eve 的默认设置(或包含在 MONGO_QUERY_BLACKLIST 中的 $where),Eve API 将返回类似于以下内容的响应:
{"_status": "ERR", "_error": {"message": "The browser (or proxy) sent a request that this server could not understand.", "code": 400}}
从黑名单中删除 $where 将返回匹配的 _items。我针对 Eve 0.7.2 对此进行了测试,以确认预期的行为。
