过滤/子请求以获得结果
Filtering / subrequest to get results
Mongodb
db.entity.find()
{
"_id" : ObjectId("5563a4c5567b3104c9ad2951"),
"section" : "section1",
"chapter" : "chapter1",
...
},
{
"_id" : ObjectId("5563a4c5567b3104c9ad2951"),
"section" : "section1",
"chapter" : "chapter2",
...
},....
在我的数据库中,集合 entity
主要包含 section
和 chapter
。只有 chapter
值是唯一的,当我们查询 mongo 给定部分时,它将 return 多个结果(一个部分与许多章节匹配)。
我需要做的是获取给定部分的所有集合,就这么简单。
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
}
}
我做错了什么?如何获取一个部分的所有实体?
试试:
curl -H 'Accept: application/json' http://127.0.0.1:5000/api/v1/entity/section23/section
您想在 url 中使用 section23
,因为它与正则表达式和存储在数据库中的实际值相匹配。
您可能还想将 url 更改为:
'url': 'entity/<regex("\w+"):section>'
这将允许 url 像这样:
http://127.0.0.1:5000/api/v1/entity/section23
这可能更具语义。希望这有帮助。
你试过这样写 url 吗:
http://127.0.0.1:5000/api/v1/entity?where=section=='section32'
请看这个python前夕。org/features.html#filtering
Mongodb
db.entity.find()
{
"_id" : ObjectId("5563a4c5567b3104c9ad2951"),
"section" : "section1",
"chapter" : "chapter1",
...
},
{
"_id" : ObjectId("5563a4c5567b3104c9ad2951"),
"section" : "section1",
"chapter" : "chapter2",
...
},....
在我的数据库中,集合 entity
主要包含 section
和 chapter
。只有 chapter
值是唯一的,当我们查询 mongo 给定部分时,它将 return 多个结果(一个部分与许多章节匹配)。
我需要做的是获取给定部分的所有集合,就这么简单。
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
}
}
我做错了什么?如何获取一个部分的所有实体?
试试:
curl -H 'Accept: application/json' http://127.0.0.1:5000/api/v1/entity/section23/section
您想在 url 中使用 section23
,因为它与正则表达式和存储在数据库中的实际值相匹配。
您可能还想将 url 更改为:
'url': 'entity/<regex("\w+"):section>'
这将允许 url 像这样:
http://127.0.0.1:5000/api/v1/entity/section23
这可能更具语义。希望这有帮助。
你试过这样写 url 吗:
http://127.0.0.1:5000/api/v1/entity?where=section=='section32'
请看这个python前夕。org/features.html#filtering