jam*_*ack 5 python pymongo bottle
@route('/locations', method='GET')
def get_location():
entity = db['locations'].find({'coordinate2d': {'$near': [37.871593, -122.272747]}}).limit(3)
if not entity:
abort(404, 'No nearby locations')
return entity
Run Code Online (Sandbox Code Playgroud)
上述代码部分的响应是:
Error 500: Internal Server Error
Sorry, the requested URL 'http://localhost:8080/locations' caused an error:
Unsupported response type: <type 'dict'>
Run Code Online (Sandbox Code Playgroud)
我如何从mongo中获取该信息,因为瓶子可以作为JSON返回?
完整的解决方案是将数据库游标转换为列表、手动设置响应类型+自定义编码返回值的组合
@route('/locations/:lat/:lng', method='GET')
def get_location(lat,lng):
response.content_type = 'application/json'
objdb = db.locations.find({'coordinate2d': {'$near': [lat,lng]}}, {'coordinate2d':bool(1)}).skip(0).limit(3)
entries = [entry for entry in objdb]
return MongoEncoder().encode(entries)
Run Code Online (Sandbox Code Playgroud)
就我而言,会产生以下结果:
[
{
"_id": "4f4201bb7e720d1dca000005",
"coordinate2d": [
33.02032100000006,
-117.19483074631853
]
},
{
"_id": "4f4201587e720d1dca000002",
"coordinate2d": [
33.158092999999994,
-117.350594
]
},
{
"_id": "4f42018b7e720d1dca000003",
"coordinate2d": [
33.195870000000006,
-117.379483
]
}
]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3869 次 |
| 最近记录: |