Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logical operators in mongodb queries with python

I am trying to query my mongo db for a item in python2.7 with

output = collection.find_one({ $and : [{'name' : data['name']},{'phone_1' : data['phone_1']}]})

when I try to run the script python tell me

File "./test.py", line 113
output = collection.find_one({ $and : [{'name' : data['name']},{'phone_1' : data['phone_1']}]})
                               ^
SyntaxError: invalid syntax

I checked the manual and the version of mongo. I have installed mongodb 2.0.6, so the syntax above should be fine. Am I missing something?

like image 543
emanuele Avatar asked Dec 24 '22 22:12

emanuele


1 Answers

Just put it into quotes:

output = collection.find_one({
    '$and': [
        {'name': data['name']},
        {'phone_1': data['phone_1']}
    ]
})
like image 61
alecxe Avatar answered Jan 04 '23 23:01

alecxe