【发布时间】:2021-10-30 09:40:48
【问题描述】:
问题陈述
我正在构建一个 API,它必须从 mongodb 数据库中检索文档,并且我需要在 Flask 应用程序中定义一个路由,以允许我在我的 mdb 查询中使用 Flask URL 变量作为值。
我尝试了以下方法:
@app.route("/infrastructure/<infrastructure_type>")
def get_infrastructure(infrastructure_type):
infrastructure = db.infrastructure.find(jsonify(f'properties.type.primary: {infrastructure_type}'), projection = {"_id": False})
return jsonify([resource for resource in infrastructure])
预期结果
我希望能够转到像 http://127.0.0.1:5000/infrastructure/mine 这样的路径,并查看数据库中主要类型为 mine 的所有文档以 JSON 形式返回。
实际结果
我收到 500 内部服务器错误和以下错误消息:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/ben/Desktop/repos/rdcep/shotgun-api/app.py", line 23, in get_infrastructure
infrastructure = db.infrastructure.find(jsonify(f'properties.type.primary: {infrastructure_type}'), projection = {"_id": False})
File "/usr/local/lib/python3.9/site-packages/pymongo/collection.py", line 1523, in find
return Cursor(self, *args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/pymongo/cursor.py", line 144, in __init__
validate_is_mapping("filter", spec)
File "/usr/local/lib/python3.9/site-packages/pymongo/common.py", line 494, in validate_is_mapping
raise TypeError("%s must be an instance of dict, bson.son.SON, or "
TypeError: filter must be an instance of dict, bson.son.SON, or any other type that inherits from collections.Mapping
来自 Mongo 的示例数据,显示属性的嵌套方式
问题
我需要做什么才能让我的查询过滤器采用 mongo 可以接受的格式?
【问题讨论】:
标签: python mongodb flask pymongo mongodb-atlas