[英]How to select a single field in MongoDB using Pymongo?
I'm trying to find a record within MongoDB, and filter _id
from the result. 我正在尝试在MongoDB中查找记录,并从结果中过滤
_id
。
Here is my code: 这是我的代码:
#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')
Output: 输出:
{'password': '123123', 'name': '<my_name>', 'email': '<my_email>', '_id': ObjectId('<an_object_id_string>')}
How do I filter the _id
field from the output? 如何从输出中过滤
_id
字段?
您需要使用投影指定要返回的字段。
data = db.author.find_one({'email' : email, 'password' : password}, {'_id': 1})
You need to pass the second object in your query. 您需要在查询中传递第二个对象。 First parameter is a select clause, whereas the second one is a projection.
第一个参数是select子句,而第二个参数是投影。
See MongoDB docs for details: https://docs.mongodb.org/manual/tutorial/project-fields-from-query-results/ 有关详细信息,请参阅MongoDB文档: https : //docs.mongodb.org/manual/tutorial/project-fields-from-query-results/
it is the best way for avoiding id,
这是避免身份证的最好方法,
data = db.author.find_one({'email' : email, 'password' : password},{"password":1, "email":1, "name":1,"_id": False})
now you got ANSWER "{'password': '123123', 'name': 'prakash', 'email': 'prakashprabhu48@gmail.com'}"(without id)
现在你得到了答案“{'密码':'123123','名字':'prakash','电子邮件':'prakashprabhu48@gmail.com'}”(没有id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.