簡體   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