【发布时间】:2018-10-10 18:57:38
【问题描述】:
我正在尝试从名为 games 的 mongo 数据库集合中访问一个新文档,该集合由 _id 提供。但是例如,如果我访问localhost:5000/solutie/5ae71f3e8e442b090e4c313b,它会给我错误:ValueError: View function did not return a response,所以它不会通过if,我认为我应该将_id 的值转换为另一种类型,但我没有不知道怎么做。
这是我的烧瓶代码:
@app.route('/solutie/<id>')
def solu(id):
games = mongo.db.games
game_user = games.find_one({'_id' : id})
if game_user:
return id
这是我名为 games 的 mongo 数据库集合:
{
"_id": {
"$oid": "5ae71f3e8e442b090e4c313b"
},
"sursa1": "nothingfornow",
"sursa2": "nothing4now",
"corectat": 0,
"player2": "test",
"player1": "test2",
"trimis1": 1,
"trimis2": 1
}
【问题讨论】:
-
在查询集合之前,您需要从字符串 id 中创建一个
ObjectId...games.find_one({'_id': ObjectId(id)})
标签: python mongodb flask pymongo