Davi
Davi

Reputation: 65

Pymongo find using $in with a list

The folowing expression works fine, returning the values that match the values list:

[...].find({"FieldName":{"$in":["Value1", "Value2", "Value3"]}})

But i have the list of values in a list object, like this:

valuesList = list()
valuesList.append("Value1")
valuesList.append("Value2")
valuesList.append("Value3")

But using the list object, a get no results:

[...].find({"FieldName":{"$in":[valuesList]}})

I Have also tried expanding the list in a formated string, like this:

strList = ', '.join(valuesList)
[...].find({"FieldName":{"$in":[strList]}})

but also, no results.

Note: If i force the list to have only one value, it works. Only when multiple values are suplied, the result is blank.

Any ideas on how to use "$in" in pymongo with a list of values in a list object?

Upvotes: 2

Views: 1283

Answers (1)

LuckyZakary
LuckyZakary

Reputation: 1191

I believe your problem is the fact that you have a list inside of a list.

Instead of:

[...].find({"FieldName":{"$in":[valuesList]}})

Try:

[...].find({"FieldName":{"$in":valuesList}})

Upvotes: 3

Related Questions