3分鐘掌握MongoDB中的regex幾種用法

3.png

3分鐘掌握MongoDB中的regex幾種用法


背景


Part1:寫在最前正則表達式

使用MySQL或其餘關係型數據庫的朋友們都知道,使用模糊查詢的用法相似於:mongodb

SELECT * FROM products WHERE sku like "%789";

本文中介紹的MongoDB中的regex就是實現相似功能的,regex爲能使你在查詢中使用正則表達式。本文會用簡單的實例帶您瞭解MongoDB中regex的用法~數據庫


Part2:用法ide

使用$regex時,有如下幾種用法:性能

{ <field>: { $regex: /pattern/, $options: '<options>' } }
{ <field>: { $regex: 'pattern', $options: '<options>' } }
{ <field>: { $regex: /pattern/<options> } }

option參數的含義:優化

選項 含義 使用要求
i 大小寫不敏感
m

查詢匹配中使用了錨,例如^(表明開頭)和$(表明結尾),以及匹配\n後的字符串url


x

忽視全部空白字符spa

要求$regex與$option合用
s 容許點字符(.)匹配全部的字符,包括換行符。 要求$regex與$option合用








實戰

Part1:$in中的用法code

要在$in查詢中包含正則表達式,只能使用JavaScript正則表達式對象(即/ pattern /)。 例如:orm

{ 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來執行如下操做:

{ name: { $regex: /acme.*corp/, $options: "si" } }
{ name: { $regex: 'acme.*corp', $options: "si" } }


Part4:索引的使用

對於區分大小寫的正則表達式查詢,若是字段存在索引,則MongoDB將正則表達式與索引中的值進行匹配,這比全表掃描更快。若是正則表達式是「前綴表達式」,那麼能夠優化查詢速度,且查詢結果都會以相同的字符串開頭。

正則表達式也要符合「最左前綴原則」,例如,正則表達式/^abc.*/將經過僅匹配以abc開頭的索引值來進行優化。


Warning: 

1.雖然/^a/,/^a.*/和/^a.*$/匹配等效字符串,但它們的性能是不同的。若是有對應的索引,全部這些表達式就都使用索引;不過,/^a.*/和/^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的結果。



——總結——

經過這幾個案例,咱們可以瞭解到MongoDB中的regex用法,以及其可選參數$option每一個選項的含義和用法。因爲筆者的水平有限,編寫時間也很倉促,文中不免會出現一些錯誤或者不許確的地方,不妥之處懇請讀者批評指正。喜歡筆者的文章,右上角點一波關注,謝謝~


1920X1080mi.jpg

33.png

相關文章
相關標籤/搜索