如何使用 Python 获取 MongoDB 中特定键的值
How to take the value of a specific key in MongoDB with Python
我相信这很简单,但我需要从键值对中提取一个值来检查它是否与会话中的用户名匹配。 (我正在使用 pymongo)
MongoDB 对象之一的示例如下所示('jam_or_event' 是集合名称):
_id: ObjectId("5d44545037b30613b88ae587")
jam_title: "Test Jam 2"
genre: "Rock"
date_of_jam: "2 August, 2019"
jam_location: "Test Address 2"
jam_county: "Bristol"
jam_member_1: "TestUser2"
member_instrument_1: "Drums"
jam_notes: "Test 2"
jam_owner: "TestUser2"
值将对象更改为对象,但我希望能够从当前选定的对象中提取 jam_owner 值,并询问它是否与当前登录用户的用户名相同。如果他们匹配,他们将在页面上拥有更多权限。
这是 python 函数的代码:
@app.route('/edit_jam/<jam_id>', methods=['POST', 'GET'])
def edit_jam(jam_id):
user_logged_in = 'username' in session
the_jam = mongo.db.jam_or_event.find_one({"_id": ObjectId(jam_id)})
username=session['username']
if request.method == 'POST':
if user_logged_in:
jams = mongo.db.jam_or_event
jams.update( {'_id': ObjectId(jam_id)},
{
'jam_title':request.form.get('jam_title'),
'genre':request.form.get('genre'),
'date_of_jam': request.form.get('date_of_jam'),
'jam_location': request.form.get('jam_location'),
'jam_county':request.form.get('jam_county'),
'jam_member_1':request.form.get('jam_member_1'),
'member_instrument_1':request.form.get('member_instrument_1'),
'jam_notes':request.form.get('jam_notes'),
})
return redirect(url_for('get_jams'))
return render_template('editjam.html',
jam=the_jam,
instruments=mongo.db.instruments.find(),
counties=mongo.db.counties.find(),
username=session['username'])
然后我想说,在 html
{% if jam_owner == username %}
xyz
{% endif %}
好的,我不确定你是不是这个意思,但我希望我理解正确,尽管问起来似乎很简单。
好的,所以这段代码从 Mongo db:
返回对象
the_jam = mongo.db.jam_or_event.find_one({"_id": ObjectId(jam_id)}) # so the_jam will hold the Object (Dictionary) from Mongodb.
您可以通过以下方式访问您的 jam_owner 或任何名称:
the_jam[key] # as key you want to get jam owner value I think so it will be the_jam["jam owner"]
并且由于您在用户名变量中有用户名,您可以比较它:
if username == the_jam["jam owner"]:
# ....
我相信这很简单,但我需要从键值对中提取一个值来检查它是否与会话中的用户名匹配。 (我正在使用 pymongo)
MongoDB 对象之一的示例如下所示('jam_or_event' 是集合名称):
_id: ObjectId("5d44545037b30613b88ae587")
jam_title: "Test Jam 2"
genre: "Rock"
date_of_jam: "2 August, 2019"
jam_location: "Test Address 2"
jam_county: "Bristol"
jam_member_1: "TestUser2"
member_instrument_1: "Drums"
jam_notes: "Test 2"
jam_owner: "TestUser2"
值将对象更改为对象,但我希望能够从当前选定的对象中提取 jam_owner 值,并询问它是否与当前登录用户的用户名相同。如果他们匹配,他们将在页面上拥有更多权限。
这是 python 函数的代码:
@app.route('/edit_jam/<jam_id>', methods=['POST', 'GET'])
def edit_jam(jam_id):
user_logged_in = 'username' in session
the_jam = mongo.db.jam_or_event.find_one({"_id": ObjectId(jam_id)})
username=session['username']
if request.method == 'POST':
if user_logged_in:
jams = mongo.db.jam_or_event
jams.update( {'_id': ObjectId(jam_id)},
{
'jam_title':request.form.get('jam_title'),
'genre':request.form.get('genre'),
'date_of_jam': request.form.get('date_of_jam'),
'jam_location': request.form.get('jam_location'),
'jam_county':request.form.get('jam_county'),
'jam_member_1':request.form.get('jam_member_1'),
'member_instrument_1':request.form.get('member_instrument_1'),
'jam_notes':request.form.get('jam_notes'),
})
return redirect(url_for('get_jams'))
return render_template('editjam.html',
jam=the_jam,
instruments=mongo.db.instruments.find(),
counties=mongo.db.counties.find(),
username=session['username'])
然后我想说,在 html
{% if jam_owner == username %}
xyz
{% endif %}
好的,我不确定你是不是这个意思,但我希望我理解正确,尽管问起来似乎很简单。
好的,所以这段代码从 Mongo db:
返回对象the_jam = mongo.db.jam_or_event.find_one({"_id": ObjectId(jam_id)}) # so the_jam will hold the Object (Dictionary) from Mongodb.
您可以通过以下方式访问您的 jam_owner 或任何名称:
the_jam[key] # as key you want to get jam owner value I think so it will be the_jam["jam owner"]
并且由于您在用户名变量中有用户名,您可以比较它:
if username == the_jam["jam owner"]:
# ....