简体   繁体   English

pymongo 查询正则表达式

[英]pymongo query regex

I am trying to retrieve all stories where the images.path has the text "images123456.jpg" for example.例如,我正在尝试检索 images.path 具有文本“images123456.jpg”的所有故事。

In my mongocompass, I am able to retrieve it using this在我的 mongocompass 中,我可以使用它来检索它

{$and: [{"images.path": {$exists: true}}, {"images.path": /.*images[1-9].*/}] }

In my python script, I tried to paste the query in the following.在我的 python 脚本中,我尝试将查询粘贴到以下内容中。

client = MongoClient(HOST, PORT)
dbStuff = client['myDatabase']   
myCollection = dbStuff.story.with_options(codec_options=CodecOptions(tz_aware=True, tzinfo=pytz.timezone('Asia/Singapore'))) 
retrieved = myCollection .find({"$and": [{"images.path": {"$exists": True}}, {"images.path": '/.*images[1-9].*/'}] })
print retrieved.count() # Prints out 0

There is something wrong in the python script for python 脚本中有问题

{"images.path": '/.*images[1-9].*/'}] }

part.部分。 How can i make the necessary changes?我怎样才能做出必要的改变?

/ is used as regular expression delimiter in some languages like JS. /在某些语言(如 JS)中用作正则表达式分隔符。 In Python you just write the contents of a regular expression as a string.在 Python 中,您只需将正则表达式的内容写为字符串。

JS: /foo.*bar/ JS: /foo.*bar/

Python: r'foo.*bar' Python: r'foo.*bar'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM