共計 3431 個字符,預計需要花費 9 分鐘才能閱讀完成。
自動寫代碼機器人,免費開通
MongoDB 中 regex 如何使用,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面丸趣 TV 小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
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 的結果。
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注丸趣 TV 行業資訊頻道,感謝您對丸趣 TV 的支持。
向 AI 問一下細節