繁体   English   中英

pymongo 查询正则表达式

[英]pymongo query regex

例如,我正在尝试检索 images.path 具有文本“images123456.jpg”的所有故事。

在我的 mongocompass 中,我可以使用它来检索它

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

在我的 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

python 脚本中有问题

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

部分。 我怎样才能做出必要的改变?

/在某些语言(如 JS)中用作正则表达式分隔符。 在 Python 中,您只需将正则表达式的内容写为字符串。

JS: /foo.*bar/

Python: r'foo.*bar'

暂无
暂无

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

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