好程序员大数据培训分享MongoDB中regex用法,Part1:写在最前
使用MySQL或其他关系型数据库的朋友们都知道,使用模糊查询的用法类似于:
SELECT*FROMproductsWHEREskulike"%789";
本文中介绍的MongoDB中的regex就是实现类似功能的,regex为能使你在查询中使用正则表达式。本文会用简单的实例带您了解MongoDB中regex的用法~
Part2:用法
使用$regex时,有以下几种用法:
{:{ r e g e x : / p a t t e r n / , regex:/pattern/, regex:/pattern/,options:’’}}
{:{ r e g e x : ′ p a t t e r n ′ , regex:'pattern', regex:′pattern′,options:’’}}
{:{$regex:/pattern/}}
option参数的含义:
选项含义使用要求
i大小写不敏感
m
查询匹配中使用了锚,例如^(代表开头)和$(代表结尾),以及匹配\n后的字符串
x
忽视所有空白字符
要求 r e g e x 与 regex与 regex与option合用
s允许点字符(.)匹配所有的字符,包括换行符。要求 r e g e x 与 regex与 regex与option合用
实战
Part1:$in中的用法
要在$in查询中包含正则表达式,只能使用JavaScript正则表达式对象(即/pattern/)。例如:
{name:{$in:[/acme/i,/ack/]}}
Warning:警告 i n 中 不 能 使 用 in中不能使用 in中不能使用regex运算符表达式。
Part2:隐式and用法
要在逗号分隔的查询条件中包含正则表达式,请使用$regex运算符。例如:
{name:{ r e g e x : / a c m e . ∗ c o r p / i , regex:/acme.*corp/i, regex:/acme.∗corp/i,nin:[‘acmeblahcorp’]}}
{name:{ r e g e x : / a c m e . ∗ c o r p / , regex:/acme.*corp/, regex:/acme.∗corp/,options:‘i’,$nin:[‘acmeblahcorp’]}}
{name:{ r e g e x : ′ a c m e . ∗ c o r p ′ , regex:'acme.*corp', regex:′acme.∗corp′,options:‘i’,$nin:[‘acmeblahcorp’]}}
Part3:x和s选项
要使用x选项或s选项,要求 r e g e x 与 regex与 regex与option合用。例如,要指定i和s选项,必须使用$options来执行以下操作:
{name:{ r e g e x : / a c m e . ∗ c o r p / , regex:/acme.*corp/, regex:/acme.∗corp/,options:“si”}}
{name:{ r e g e x : ′ a c m e . ∗ c o r p ′ , regex:'acme.*corp', 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”:“Singlelinedescription.”}
{"_id":101,“sku”:“abc789”,“description”:“Firstline\nSecondline”}
{"_id":102,“sku”:“xyz456”,“description”:“Manyspacesbeforeline”}
{"_id":103,“sku”:“xyz789”,“description”:“Multiple\nlinedescription”}
如果想对该商品products集合执行一个查询,范围是sku列中的内容是789结尾的:
db.products.find({sku:{ r e g e x : / 789 regex:/789 regex:/789/}})
结合MySQL理解的话,上述查询在MySQL中是这样的SQL:
SELECT*FROMproductsWHEREskulike"%789";
如果想查询sku是abc、ABC开头的,且匹配时忽略大小写,可以使用i选项:
db.products.find({sku:{$regex:/^ABC/i}})、
查询结果为:
{"_id":100,“sku”:“abc123”,“description”:“Singlelinedescription.”}
{"_id":101,“sku”:“abc789”,“description”:“Firstline\nSecondline”}
Part6:m的使用
想查询描述中是包含S开头的,且要匹配/n后的S开头的,则需要加m选项
db.products.find({description:{ r e g e x : / S / , regex:/^S/, regex:/S/,options:‘m’}})
返回的结果是:
{"_id":100,“sku”:“abc123”,“description”:“Singlelinedescription.”}
{"_id":101,“sku”:“abc789”,“description”:“Firstline\nSecondline”}
如果不加m选项的话,返回的结果是这样的:
{"_id":100,“sku”:“abc123”,“description”:“Singlelinedescription.”}
如果不使用^这类锚的话,那么会返回全部结果:
db.products.find({description:{$regex:/S/}})
{"_id":100,“sku”:“abc123”,“description”:“Singlelinedescription.”}
{"_id":101,“sku”:“abc789”,“description”:“Firstline\nSecondline”}
Part7:s的使用
使用s选项来执行查询,则会让逗号.匹配所有字符,包括换行符,下文查询了description列中m开头,且后面包含line字符串的结果:
db.products.find({description:{ r e g e x : / m . ∗ l i n e / , regex:/m.*line/, regex:/m.∗line/,options:‘si’}})
{"_id":102,“sku”:“xyz456”,“description”:“Manyspacesbeforeline”}
{"_id":103,“sku”:“xyz789”,“description”:“Multiple\nlinedescription”}
如果不包含s,则会返回:
{"_id":102,“sku”:“xyz456”,“description”:“Manyspacesbeforeline”}
Part8:x的使用
以下示例使用x选项忽略空格和注释,用#表示注释,并以匹配模式中的\n结尾:
varpattern=“abc#categorycode\n123#itemnumber”
db.products.find({sku:{ r e g e x : p a t t e r n , regex:pattern, regex:pattern,options:“x”}})
查询的结果是:
{"_id":100,“sku”:“abc123”,“description”:“Singlelinedescription.”}
可以看出,其忽略了abc与#category的空格以及#category与code的空格,实际执行的查询是sku是abc123的结果。