如何基于 Flask URL 变量使用 PyMongo 检索 MongoDB 中的记录?

How do I retrieve records in MongoDB with PyMongo based on Flask URL variables?

问题陈述

我正在构建一个必须从 mongodb 数据库中检索文档的 API,我需要在 Flask 应用程序中定义一个允许我使用 Flask URL 变量作为我的 mdb 查询中的值。

我试过以下方法:

@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 可以接受的格式?

我建议您直接传递一个 dict 作为您的过滤器参数而不是 jsonify(f'properties.type.primary: {infrastructure_type}')

示例:

@app.route("/infrastructure/<infrastructure_type>")
def get_infrastructure(infrastructure_type):
    infrastructure = db.infrastructure.find({'properties': {'type': {'primary': infrastructure_type}}}, projection = {"_id": False})
    return jsonify([resource for resource in infrastructure])

Alexandre Mahdhaoui in 所述,我需要传递一个 dict 作为我的过滤器参数,但出于某种原因,我的数据库需要点符号来访问。此答案包含完整的解决方案:

@app.route("/infrastructure/<infrastructure_type>")
def get_infrastructure(infrastructure_type):
    infrastructure = db.infrastructure.find({'properties.type.primary': infrastructure_type}, projection = {"_id": False})
    return jsonify([resource for resource in infrastructure])