Caaguazu
Caaguazu

Reputation: 113

PYTHON - PYMONGO - Invalid Syntax with $or

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)

Upvotes: 1

Views: 1900

Answers (1)

Caaguazu
Caaguazu

Reputation: 113

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)

Upvotes: 8

Related Questions