MongoDB中regex用法介紹

來源:互聯網
上載者:User

MongoDB中regex用法介紹

背景

Part1:寫在最前

使用MySQL或其他關係型資料庫的朋友們都知道,使用模糊查詢的用法類似於:
SELECT * FROM products WHERE sku like "%789";

本文中介紹的MongoDB中的regex就是實作類別似功能的,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查詢中包含Regex,只能使用JavaScriptRegex對象(即/ pattern /)。 例如:
{ name: { $in: [ /^acme/i, /^ack/ ] } }

Warning:警告 $in中不能使用$ regex運算子運算式。

Part2:隱式and用法

要在逗號分隔的查詢條件中包含Regex,請使用$ 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:索引的使用

對於區分大小寫Regex查詢,如果欄位存在索引,則MongoDB將Regex與索引中的值進行匹配,這比全表掃描更快。如果Regex是“首碼運算式”,那麼可以最佳化查詢速度,且查詢結果都會以相同的字串開頭。

Regex也要符合“最左首碼原則”,例如,Regex/^abc.*/將通過僅匹配以abc開頭的索引值來進行最佳化。

Warning:警告

1.雖然/^a/,/^a.*/和/^a.*$/匹配等效字串,但它們的效能是不一樣的。如果有對應的索引,所有這些運算式就都使用索引;不過,/^a.*/和/^a.*$/較慢。 這是因為/^a/可以在匹配首碼後停止掃描。

2.不區分大小寫Regex查詢通常不能使用索引,$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每個選項的含義和用法。

更多MongoDB相關教程見以下內容:

MongoDB文檔、集合、資料庫簡介 

MongoDB 3分區部署及故障類比驗證 

Linux CentOS 6.5 yum安裝MongoDB 

CentOS 7 yum方式快速安裝MongoDB 

MongoDB的查詢操作 

在 Azure 虛擬機器上快速搭建 MongoDB 叢集 

MongoDB複製集原理 

MongoDB 3.4 遠端連線認證失敗 

Ubuntu 16.04中安裝MongoDB3.4資料庫系統 

MongoDB權威指南第2版PDF完整帶書籤目錄 下載見

MongoDB 的詳細介紹:請點這裡
MongoDB 的:請點這裡

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.