選項 | 含義 | 使用要求 |
---|---|---|
i | 大小寫不敏感 | |
m |
查詢匹配中使用了錨,例如^(代表開頭)和$(代表結尾),以及匹配\n后的字符串 |
|
x |
忽視所有空白字符 |
要求$regex與$option合用 |
s | 允許點字符(.)匹配所有的字符,包括換行符。 | 要求$regex與$option合用 |
實戰(zhàn)
Part1:$in中的用法
要在$in查詢中包含正則表達式,只能使用JavaScript正則表達式對象(即/ pattern /)。 例如:
{ name: { $in: [ /^acme/i, /^ack/ ] } }
Warning:警告 $in中不能使用$ regex運算符表達式。
Part2:隱式and用法
要在逗號分隔的查詢條件中包含正則表達式,請使用$ regex運算符。 例如:
{ name: { $regex: /acme.*corp/i, $nin: [ 'acmeblahcorp' ] } } { name: { $regex: /acme.*corp/, $options: 'i', $nin: [ 'acmeblahcorp' ] } } { name: { $regex: 'acme.*corp', $options: 'i', $nin: [ 'acmeblahcorp' ] } }
Part3:x和s選項
要使用x選項或s選項,要求$regex與$option合用。 例如,要指定i和s選項,必須使用$ options來執(zhí)行以下操作:
{ name: { $regex: /acme.*corp/, $options: "si" } } { name: { $regex: 'acme.*corp', $options: "si" } }
Part4:索引的使用
對于區(qū)分大小寫的正則表達式查詢,如果字段存在索引,則MongoDB將正則表達式與索引中的值進行匹配,這比全表掃描更快。如果正則表達式是“前綴表達式”,那么可以優(yōu)化查詢速度,且查詢結果都會以相同的字符串開頭。
正則表達式也要符合“最左前綴原則”,例如,正則表達式/^abc.*/將通過僅匹配以abc開頭的索引值來進行優(yōu)化。
Warning:警告
1.雖然/^a/,/^a.*/和/^a.*$/匹配等效字符串,但它們的性能是不一樣的。如果有對應的索引,所有這些表達式就都使用索引;不過,/^a.*/和/^a.*$/較慢。 這是因為/^a/可以在匹配前綴后停止掃描。
2.不區(qū)分大小寫的正則表達式查詢通常不能使用索引,$regex無法使用不區(qū)分大小寫的索引。
Part5:實例
一個商品的集合中,存了以下內容
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." } { "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" } { "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before line" } { "_id" : 103, "sku" : "xyz789", "description" : "Multiple\nline description" }
如果想對該商品products集合執(zhí)行一個查詢,范圍是sku列中的內容是789結尾的:
db.products.find( { sku: { $regex: /789$/ } } )
結合MySQL理解的話,上述查詢在MySQL中是這樣的SQL:
SELECT * FROM products WHERE sku like "%789";
如果想查詢sku是abc、ABC開頭的,且匹配時忽略大小寫,可以使用i選項:
db.products.find( { sku: { $regex: /^ABC/i } } )、
查詢結果為:
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." } { "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }
Part6:m的使用
想查詢描述中是包含S開頭的,且要匹配/n后的S開頭的,則需要加m選項
db.products.find( { description: { $regex: /^S/, $options: 'm' } } )
返回的結果是:
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." } { "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }
如果不加m選項的話,返回的結果是這樣的:
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
如果不使用^這類錨的話,那么會返回全部結果:
db.products.find( { description: { $regex: /S/ } } ) { "_id" : 100, "sku" : "abc123", "description" : "Single line description." } { "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }
Part7:s的使用
使用s選項來執(zhí)行查詢,則會讓逗號. 匹配所有字符,包括換行符,下文查詢了description列中m開頭,且后面包含line字符串的結果:
db.products.find( { description: { $regex: /m.*line/, $options: 'si' } } ) { "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before line" } { "_id" : 103, "sku" : "xyz789", "description" : "Multiple\nline description" }
如果不包含s,則會返回:
{ "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before line" }
Part8:x的使用
以下示例使用x選項忽略空格和注釋,用#表示注釋,并以匹配模式中的\ n結尾:
var pattern = "abc #category code\n123 #item number" db.products.find( { sku: { $regex: pattern, $options: "x" } } )
查詢的結果是:
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
可以看出,其忽略了abc與#category的空格以及#category與code的空格,實際執(zhí)行的查詢是sku是abc123的結果。
總結
通過這幾個案例,我們能夠了解到MongoDB中的regex用法,以及其可選參數(shù)$option每個選項的含義和用法。由于筆者的水平有限,編寫時間也很倉促,文中難免會出現(xiàn)一些錯誤或者不準確的地方,不妥之處懇請讀者批評指正。
好了,以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。