JamesHudson81
JamesHudson81

Reputation: 2273

Inserting in inside a function

I am a newbie at interacting with mongo and I am trying to make a function using the in property:

def find_inthe(key,*values):
    rdo=col.find({key:{'$in':list(values)}})
    for a in rdo:
        return a

print(find_inthe('hair_colour','white','brown','black'))

However the problem I am finding is that it is returning only the first of the values and as a find_one.

If I try the same outside the function:

rdo=col.find({'hair_colour':{'$in':['white','brown','black']}})          
for a in rdo:
    print(a)

It will return all the dictionaries of the collection.

My desired output would be that the function returns all the dictionaries with the selected colour hairs

Upvotes: 0

Views: 22

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230386

$in is not at fault here. It's your return a in the loop. Why don't you return the whole thing?

def find_inthe(key,*values):
    rdo = col.find({key:{'$in':list(values)}})
    return list(rdo)

Upvotes: 2

Related Questions