背景正則表達式
Part1:寫在最前數據庫
使用MySQL或其餘關係型數據庫的朋友們都知道,使用模糊查詢的用法相似於:bash
SELECT * FROM products WHERE sku like "%789";
本文中介紹的MongoDB中的regex就是實現相似功能的,regex爲能使你在查詢中使用正則表達式。本文會用簡單的實例帶您瞭解MongoDB中regex的用法~性能
Part2:用法優化
使用$regex時,有如下幾種用法:spa
{ <field>: { $regex: /pattern/, $options: '<options>' } }
{ <field>: { $regex: 'pattern', $options: '<options>' } }
{ <field>: { $regex: /pattern/<options> } }
複製代碼
option參數的含義:code
實戰
Part1:$in中的用法cdn
要在$in查詢中包含正則表達式,只能使用JavaScript正則表達式對象(即/ pattern /)。 例如:對象
{ name: { in中不能使用$ regex運算符表達式。blog
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選項,要求option合用。 例如,要指定i和s選項,必須使用$ options來執行如下操做:
{ name: { $regex: /acme.*corp/, $options: "si" } }
{ name: { $regex: 'acme.*corp', $options: "si" } }
複製代碼
Part4:索引的使用
對於區分大小寫的正則表達式查詢,若是字段存在索引,則MongoDB將正則表達式與索引中的值進行匹配,這比全表掃描更快。若是正則表達式是「前綴表達式」,那麼能夠優化查詢速度,且查詢結果都會以相同的字符串開頭。
正則表達式也要符合「最左前綴原則」,例如,正則表達式/^abc.*/將經過僅匹配以abc開頭的索引值來進行優化。
**Warning:警告 **
1.雖然/a/,/a.
2.不區分大小寫的正則表達式查詢一般不能使用索引,$regex沒法使用不區分大小寫的索引。
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集合執行一個查詢,範圍是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選項來執行查詢,則會讓逗號. 匹配全部字符,包括換行符,下文查詢了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的空格,實際執行的查詢是sku是abc123的結果。