简体   繁体   English

Pymongo:如何获得特定领域的价值

[英]Pymongo: how to get the value of a specific field

I have the document below. 我有下面的文件。 I would like to get the topic field if the password matches. 如果密码匹配,我想获取主题字段。

email: "info@info.com" 电子邮件:“info@info.com”
first_name: "james" first_name:“詹姆斯”
password: Binary('HcM0Js5Pqh8oGZxLSNuMJNgb5l1WUCn5NRhB/L4juIY=') 密码:二进制('HcM0Js5Pqh8oGZxLSNuMJNgb5l1WUCn5NRhB / L4juIY =')
surname:"mcarthy" topic: "journalism" 姓:“mcarthy”主题:“新闻”

I am using below code to check if the key exists. 我使用下面的代码来检查密钥是否存在。

    user = user_in_db.find_one({'password': key})
    if user is None:

How can i retrieve the topic field if the user does exist? 如果用户确实存在,我如何检索主题字段?

That is very simple. 这很简单。 As per this official tutorial , the return of find_one is a dictionary, hence you could do something like: 根据这个官方教程find_one的返回是一个字典,因此你可以这样做:

user = user_in_db.find_one({'password': key})
if user:  # check if user exists
    topic = user['topic']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM