问题
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