Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pymongo.find() only return answer

I am working on a code which will fetch data from the database using pymongo. After that I'll show it in a GUI using Tkinter.

I am using

.find()

to find specific documents. However I don't want anything else then 'name' to show up. So I used {"name":1}, now it returns:

{u'name':u'**returned_name**'}

How do I remove the u'name': so it will only return returned_name?

Thanks in advance,

Max

P.s. I have searched a lot around the web but couldn't find anything which would give me some argument to help me.

like image 443
Maxim Avatar asked Feb 16 '16 14:02

Maxim


Video Answer


1 Answers

What you see returned by find() call is a cursor. Just iterate over the cursor and get the value by the name key for every document found:

result = db.col.find({"some": "condition"}, {"name": 1})
print([document["name"] for document in result])

As a result, you'll get a list of names.

Or, if you want and expect a single document to be matched, use find_one():

document = db.col.find_one({"some": "condition"}, {"name": 1})
print(document["name"])
like image 132
alecxe Avatar answered Sep 24 '22 03:09

alecxe