2012-12-22 83 views
0

比方說,我們有一個集合初始化一個條目MongoDB的正則表達式查詢返回任何結果

db.test.insert({text:"why does this not work"}) 

所以它看起來像這樣

db.test.find() 
{ "_id" : ObjectId("50d500b384a6859f9a9bfbc1"), "text" : "why does this not work" } 

而且它看起來像

db.test.find({"text":/.*why.*/}) 
查詢

我期待這會收錄第一張唱片,但事實並非如此。任何想法爲什麼不呢?

+0

當我嘗試時工作正常。但是不管怎樣,'/ * /'部件都是以相同的方式匹配的。 – JohnnyHK

回答

0

Javascript不支持'。'。改爲使用[\ s \ S]。這實際上意味着:無論是空間還是不是空間。

編輯:雖然你的例子工作得很好。我遇到了上述的Javascript問題,[\ s \ S]修復了它。去嘗試一下。

+0

Javascript正則表達式絕對支持'.'字符類。如果用在大括號內,它將意味着一個文字時期,但。也許這是問題? –

+2

你說得對。我遇到的問題是不同的:。在[\ s \ S]中不符合換行符。在這種情況下,沒有換行符。 – sqreept

0

它應該工作得很好。

MongoDB shell version: 2.2.0 
connecting to: test 
> db.test.insert({text: "this is some sample text"}) 
> db.test.find() 
{ "_id" : ObjectId("50d50b85499df584cc266f9a"), "text" : "this is some sample text" } 
> db.test.find({text: /sample/}) 
{ "_id" : ObjectId("50d50b85499df584cc266f9a"), "text" : "this is some sample text" } 
> db.test.find({text: /.*sample.*/}) 
{ "_id" : ObjectId("50d50b85499df584cc266f9a"), "text" : "this is some sample text" } 

即使你確切的樣本工程:

> db.test.insert({text:"why does this not work"}) 
> db.test.find({"text":/.*why.*/}) 
{ "_id" : ObjectId("50d50c4c499df584cc266f9b"), "text" : "why does this not work" } 

也許你已經忽略了別的東西?

相關問題