PYTHON - PYMONGO - Invalid Syntax with $or

隐身守侯 提交于 2019-12-11 06:07:20

问题


I can't seem to get this to work with pymongo it was working before I added the $or option. Am I missing something obvious with this

dataout = releasescollection.find( { $or: [{"l_title":{"$regex": "i walk the line", "$options": "-i"}}, {"artistJoins.0.artist_name":{"$regex": "Johnny Cash", "$options": "-i"}}]}).sort('id', pymongo.ASCENDING).limit(25)

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "discogs.py", line 51 dataout = releasescollection.find( { $or: [{"l_title":{"$regex": "i walk the line", "$options": "-i"}}, {"artistJoins.0.artist_name":{"$regex": "Johnny Cash", "$options": "-i"}}]}) ^ SyntaxError: invalid syntax

Running the below directly in mongo works but I'm missing something in the switchover to python

db.releases.find( { $or: [{"l_title":{"$regex": "i walk the line", "$options": "-i"}}, {"artistJoins.0.artist_name":{"$regex": "Johnny Cash", "$options": "-i"}}]}).sort({'id':1}).limit(25)


回答1:


Should've relized this far sooner, once I added the $or it needs to be in quotes. So this works:

dataout = releasescollection.find( { "$or": [{"l_title":{"$regex": "i walk the line", "$options": "-i"}}, {"artistJoins.0.artist_name":{"$regex": "Johnny Cash", "$options": "-i"}}]}).sort('id', pymongo.ASCENDING).limit(25)



来源:https://stackoverflow.com/questions/49204190/python-pymongo-invalid-syntax-with-or

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!