PYTHON-PYMONGO-带有$ or的无效语法 [英] PYTHON - PYMONGO - Invalid Syntax with $or
问题描述
在添加$ or选项之前,我似乎无法使其与pymongo一起使用.我是否错过了一些明显的东西
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
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
直接在mongo中运行下面的命令是可行的,但是在切换到python时缺少了一些东西
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)
推荐答案
一旦我添加了$或它需要用引号引起来,就应该早点解决这个问题.如此有效:
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)
这篇关于PYTHON-PYMONGO-带有$ or的无效语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!