Search code examples
pythonmongodbtornado-motor

OR condition in motor python


Hi I am experimenting with python and mongodb with tornado framework. I am having entry module where user can insert the data of students in academic and sports field. In mongodb terminal I did search with

db.student.find( { $or: [ { "academy": name  }, { "sports": name } ] } )

but when I try to do the same with python along with MOTOR driver I end up with error.

My python command is

doc = yield db.student.find_one({ $or: [{"academy": name}, {"sports": name}]})

Can anyone guide me how I can do the search with or condition in python motor?

The or condition is used to check whether the data of particular student is entered in both the fields or not.


Solution

  • You write, "I end up with an error", but it is very difficult for anyone to answer your question if you don't tell us what the error is!

    In this particular case I think I know the problem. In Python, all field names must be quoted. The proper syntax is:

    doc = yield db.student.find_one({ "$or": [{"academy": name}, {"sports": name}]})