2015-05-04 100 views
2

我剛開始使用mongodb並設置了一個測試數據庫來處理來自我創建的幾個腳本的網頁抓取結果。現在,date_found被作爲一個字符串加載。當我在mongohub運行此:使用pymongo查詢mongodb

{"date_found" : /.*2015-05-02.*/} 

我得到所有以「2015年5月2日」的收藏品。真棒!

然而,當我運行:

for item in collection.find({"date_found": "/.*2015-05-02.*/"}): 
    print item 

我什麼也得不到。

也,這樣的:

for item in collection.find(): 
    print item 

給了我所有的收藏品,因此它似乎一切正常,到我可以查詢數據庫的程度。

任何機會有人可以告訴我我正在做什麼(或我失蹤)的骨頭錯誤?

回答

2

在pymongo,包括一個regular expression你可以嘗試這樣的事:

import re 
regx = re.compile(".*2015-05-02.*") 
for item in collection.find({"date_found": regx}) 
    print item 

或者使用$regex操作:

import re 
regx = re.compile(".*2015-05-02.*") 
for item in collection.find({"date_found": {"$regex": regx} }) 
    print item 
+0

這兩個都拋出「sre_constants.error:沒有什麼可重複的「在regx線...... – Silas

+2

看起來像這些正則表達式字符串中缺少前導'.'。 – JohnnyHK

+0

我已經更新了答案,包括領先的'.'。感謝@JohnnyHK提供的單挑。 – chridam