python/ pymongo

I have the document below. I would like to get the topic field if the password matches.

email: "info@info.com"
first_name: "james"
password: Binary('HcM0Js5Pqh8oGZxLSNuMJNgb5l1WUCn5NRhB/L4juIY=')
surname:"mcarthy" topic: "journalism"

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:

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

暂无
暂无

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Pymongo - How can I return a specific field? How to manipulate a specific field in a document (PyMongo) Pymongo How to update specific value of row in array How to get only value in pymongo instead of ObjectId PyMongo get to find json object based on field value How to find specific key:value 2 from python pymongo, MongoDB how to get an input in Pymongo how to print field in chinese in pymongo How to query nested field with pymongo Pymongo: How to check if field exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM