如何使用Pymongo在MongoDB中选择单个字段? [英] How to select a single field in MongoDB using Pymongo?
本文介绍了如何使用Pymongo在MongoDB中选择单个字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在MongoDB中查找一条记录,并从结果中过滤_id
.
I'm trying to find a record within MongoDB, and filter _id
from the result.
这是我的代码:
#app.py
@app.route('/login', methods = ['GET', 'POST'])
def login():
if request.method == "POST":
password = request.form.get('password')
email = request.form.get('email')
db = get_db()
data = db.author.find_one({'email' : email, 'password' : password})
print(data)
return 'data'
else:
return render_template('login.html')
输出:
{'password': '123123', 'name': '<my_name>', 'email': '<my_email>', '_id': ObjectId('<an_object_id_string>')}
如何从输出中过滤_id
字段?
How do I filter the _id
field from the output?
推荐答案
您需要使用投影指定要返回的字段.
You need to specify the field you want to return using projection.
data = db.author.find_one({'email' : email, 'password' : password}, {'_id': 1})
这篇关于如何使用Pymongo在MongoDB中选择单个字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文