Darwin
Darwin

Reputation: 519

Pymongo query issue

I have a query that works in the mongo client but doesn't return anything when using pymongo.

I have tried:

posts = collection.find({"species": argv[0]}), "evidence" : {"$in":["[true,true,true,null]","[true,null,true,null]"]}})

and

 posts = collection.find({"species": argv[0]}), "evidence" : {"$in":   [["true","true","true","null"],["true","null","true","null"]]}})

I have narrowed the problem to the $in statement looking for different arrays, because if I run this it works:

 posts = collection.find({"species": argv[0]})})

Upvotes: 1

Views: 83

Answers (2)

Darwin
Darwin

Reputation: 519

Thanks to alecxe for catching a syntax error. However, the root of the problem was caused by odd behavior. Within the mongo database the values in the evidence array are true, false, or null (e.g. [true, false, null, null]. If I search like this:

posts = collection.find({"species": argv[0], "evidence" : {"$in": [["true","true","true","null"],["true","null","true","null"]]}})

I get no results. If I search changing the arrays values to python keywords like this:

posts = collection.find({"species": argv[0], "evidence" : {"$in": [[True,None,None,None], [True,None,True,None]]}})

It returns the proper results.

Upvotes: 1

alecxe
alecxe

Reputation: 474241

Looks like you've just mixed up parenthesis and braces:

posts = collection.find({"species": argv[0], 
                         "evidence" : {"$in": [["true","true","true","null"]         
                                               ["true","null","true","null"]]}})

Upvotes: 1

Related Questions