Lemon553311-dev
Lemon553311-dev

Reputation: 77

How to take the value of a key in mongodb using python

I am new to mongoDB. I am using pymongo. Here I have an example list of favorite colors stored in a mongoDB collection named colors:

mylist = [
    {"name": "Amy", "color": "Red"},
    {"name": "Hannah", "color": "Blue"},
    {"name": "Michael", "color": "Green"},
    {"name": "Sarah", "color": "Orange"}
]

Using the search query how do I take the value "color" using query searching using a name like Michael? So the output would be like:

Green

Upvotes: 1

Views: 117

Answers (1)

Ravi Shankar Bharti
Ravi Shankar Bharti

Reputation: 9268

You can do it something like this:

Assuming that name is unique, you can try this:

db.colors.find_one({"name" : "Michael"})["color"]

You can read more about find_one here

Upvotes: 1

Related Questions