1. 程式人生 > >3分鐘掌握MongoDB中的regex幾種用法

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

lib cat 正則表達式 小寫 可選參數 介紹 src 我們 包括

技術分享圖片

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


背景


Part1:寫在最前

使用MySQL或其他關系型數據庫的朋友們都知道,使用模糊查詢的用法類似於:

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

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


Part2:用法

使用$regex時,有以下幾種用法:

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

option參數的含義:

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

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


x

忽視所有空白字符

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








實戰

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來執行以下操作:

{ 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每個選項的含義和用法由於者的水平有限,編寫時間也很倉促,文中難免會出現一些錯誤或者不準確的地方,不妥之處懇請讀者批評指正。


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