Skip to main content
 首页 » 编程设计

python之过滤/子请求以获得结果

2025年05月04日10duanxz

Mongodb

 db.entity.find() 
{ 
        "_id" : ObjectId("5563a4c5567b3104c9ad2951"), 
        "section" : "section1", 
        "chapter" : "chapter1", 
        ... 
}, 
{ 
        "_id" : ObjectId("5563a4c5567b3104c9ad2951"), 
        "section" : "section1", 
        "chapter" : "chapter2", 
        ... 
},.... 

在我的数据库中,entity 集合主要包含section 和chapter。只有 chapter 值是唯一的,当我们为给定部分查询 mongo 时,它将返回多个结果(一个部分与许多章节匹配)。

我需要做的是获取给定部分的所有集合,就这么简单。

settings.py

URL_PREFIX = "api" 
 
API_VERSION = "v1" 
 
ALLOWED_FILTERS = ['*'] 
 
schema = { 
    'section': { 
        'type': 'string', 
    },   
    'chapter': { 
        'type': 'string', 
    }, 
} 
 
entity = { 
    'item_title': 'entity', 
    'resource_methods': ['GET'], 
 
    'cache_control': '', 
    'cache_expires': 0, 
 
    'url': 'entity/<regex("\w+"):section>/section', 
 
    'schema': schema 
} 
 
DOMAIN = { 
    'entity': entity, 
} 

run.py

from eve import Eve 
 
if __name__ == '__main__':                       
    app.run(debug=True)    

What I tried

curl -L -X GET -H 'Content-Type: application/json' http://127.0.0.1:5000/api/v1/entity/23/section 

OUTPUT

{  
  "_status": "ERR", 
  "_error": { 
    "message": "The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again.", 
    "code": 404 
  } 
} 

我做错了什么?如何获取一个部分的所有实体?

请您参考如下方法:

你试过这样写url吗:

http://127.0.0.1:5000/api/v1/entity?where=section=='section32' 

请看看这个 python-eve.org/features.html#filtering