Mongodb正则表达式$regex操作符

原文地址:https://blog.csdn.net/yaomingyang/article/details/78794155

一、$regex为模糊查询的字符串提供正则表达式功能,MongoDB使用Perl兼容正则表达式(即“文件”)8.41版与UTF-8支持。

使用$regex操作符要使用如下语法:

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

在mongodb中你也可以使用正则表达式对象(/parttern/)来匹配

{ <field>: /pattern/<options> }

二、$options选项,一下的<options>选项是适合正则表达式的

Option Description Syntax Restrictions
i Case insensitivity to match upper and lower cases. For an example, seePerform Case-Insensitive Regular Expression Match.  
m

For patterns that include anchors (i.e. ^ for the start, $ for the end), match at the beginning or end of each line for strings with multiline values. Without this option, these anchors match at beginning or end of the string. For an example, see Multiline Match for Lines Starting with Specified Pattern.

If the pattern contains no anchors or if the string value has no newline characters (e.g. \n), the m option has no effect.

 
x

“Extended” capability to ignore all white space characters in the$regex pattern unless escaped or included in a character class.

Additionally, it ignores characters in-between and including an un-escaped hash/pound (#) character and the next new line, so that you may include comments in complicated patterns. This only applies to data characters; white space characters may never appear within special character sequences in a pattern.

The x option does not affect the handling of the VT character (i.e. code 11).

Requires $regex with $optionssyntax
s Allows the dot character (i.e. .) to match all characters includingnewline characters. For an example, see Use the . Dot Character to Match New Line. Requires $regex with $optionssyntax

三、规则 $regex vs. /pattern/ Syntax

在一个$in的查询表达式中使用正则表达式,你只能使用JavaScript正则表达式(i.e. /pattern/ ),如下:

{ name: { $in: [ /^acme/i, /^ack/ ] } }

你不可以使用$regex操作符正则表达式在$in查询条件中:

隐含的字段条件

在一个逗号分隔的查询条件列表中包含正则表达式,使用$regex操作符:

{ name: { $regex: /acme.*corp/i, $nin: [ 'acmeblahcorp' ] } }
{ name: { $regex: /acme.*corp/, $options: 'i', $nin: [ 'acmeblahcorp' ] } }
{ name: { $regex: 'acme.*corp', $options: 'i', $nin: [ 'acmeblahcorp' ] } }

为了使用x选项和s选项你必须使用$regex操作符表达式和$options操作符表达式,例如:指定i和s选项,你必须同时使用$options选项;

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

四、PCRE(Perl compatible regular Expression) vs Javascript

使用PCRE支持的正则表达式特性不支持用在Javascript中,你必须使用$regex操作符以字符串的形式,例如:使用(?i)在这种模式中对其它模式进行大小写忽略和(?-i)为了剩余模式的大小写灵敏度,你必须以字符串使用$regex操作符;

{ name: { $regex: '(?i)a(?-i)cme' } }


四、对于区分大小写的正则表达式查询,如果字段存在索引,然后mongodb匹配正则表达式中对应索引的值,他可以比集合扫描更快,如果这个正则表达式是一个前缀正则表达式有进一步优化的空间,这就意味着所有潜在匹配都以相同的字符串开始;

如果开始以插入符号(^)或以一个左锚(\A)这样的正则表达式是一个前缀表达式;接着是一串简单的字符串,例如:正则表达式/^abc.*/将通过匹配优化只针对以abc开始的索引值;

此外, /^a/, /^a.*/, and /^a.*$/匹配等价的字符串,他们具有不同的性能特征,如果适当的索引存在,所有这些表达式都使用索引; however, /^a.*/, and /^a.*$/ are slower. /^a/ can stop scanning after matching the prefix.

不区分大小写的正则表达式查询一般不能有效地使用索引。$regex操作符实现不符合排序规则,无法利用不区分大小写的索引。


如下的products集合文档使用如下的:

{ "_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" }

演示一个LIKE匹配

如下的例子匹配所有文档中sku字段"%789"

db.products.find( { sku: { $regex: /789$/ } } )

该示例类似于以下类似SQL语句:

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

演示忽略大小写正则表达式匹配:

如下的例子演示了一个试用选项i忽略文档中sku字段的大小写匹配并且以ABC开头;

db.products.find( { sku: { $regex: /^ABC/i } } )

查询匹配的结果是:

{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
{ "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }

下面的例子使用选项m匹配字母多行以S开头的字符串:

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." }

如果$regex不包含任何锚点,模式作为字符串进行整体匹配,如下面的示例所示:

db.products.find( { description: { $regex: /S/ } } )

然后,$regex将会匹配如下文档:

{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
{ "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值