简体   繁体   English

如何使用Pymongo在MongoDB中选择单个字段?

[英]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.

相关问题 MongoDB和PyMongo - 如何在单个文档字段内连接字符串 - MongoDB and PyMongo - how to concat string inside single document field 如何使用pymongo用单个文档更新mongodb集合? - How to update a mongodb collection with single document using pymongo? 如何使用Pymongo在MongoDB中的fs.chunks集合中添加字段? - How to add a field in fs.chunks collection in MongoDB using Pymongo? mongodb:如何使用 python (pymongo) 创建计算字段? - mongodb: how to create a calculated field using python (pymongo)? 如何使用mongokit / pymongo填充wtform select字段? - How to populate wtform select field using mongokit/pymongo? 在pymongo中选择*在MongoDB中限制100个等效项 - select * limit 100 equivalent in MongoDB using pymongo 如何使用 python mongodb 客户端库 (pymongo) 更新 mongodb 集合中所有文档的字段“类型” - How to update field “type” for all documents in mongodb collection using python mongodb client library (pymongo) 如何使用 pymongo 重命名 mongodb 中的父字段名称和嵌套字段值? - How do I rename parent field name and nested field value in mongodb using pymongo? 如何使用pymongo根据python中的字段比较MongoDB中的记录的唯一性? - How do I compare records in MongoDB for uniqueness based on a field in python using pymongo? 在使用pymongo在mongodb中使用upsert将查询更新为true后,如何返回记录字段? - How to return field of record after update query with upsert as true in mongodb using pymongo?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM