赞
踩
一、$regex为模糊查询的字符串提供正则表达式功能,MongoDB使用Perl兼容正则表达式(即“文件”)8.41版与UTF-8支持。
使用$regex操作符要使用如下语法:
{ <field>: { $regex: /pattern/, $options: '<options>' } }
{ <field>: { $regex: 'pattern', $options: '<options>' } }
{ <field>: { $regex: /pattern/<options> } }
{ <field>: /pattern/<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. If the pattern contains no anchors or if the string value has no newline characters (e.g. | |
x | “Extended” capability to ignore all white space characters in the Additionally, it ignores characters in-between and including an un-escaped hash/pound ( The | Requires $regex with $options syntax |
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 $options syntax |
三、规则 $regex vs. /pattern/ Syntax
在一个$in的查询表达式中使用正则表达式,你只能使用JavaScript正则表达式(i.e. /pattern/ ),如下:
{ name: { $in: [ /^acme/i, /^ack/ ] } }
隐含的字段条件
在一个逗号分隔的查询条件列表中包含正则表达式,使用$regex操作符:
{ name: { $regex: /acme.*corp/i, $nin: [ 'acmeblahcorp' ] } }
{ name: { $regex: /acme.*corp/, $options: 'i', $nin: [ 'acmeblahcorp' ] } }
{ name: { $regex: 'acme.*corp', $options: 'i', $nin: [ 'acmeblahcorp' ] } }
{ name: { $regex: /acme.*corp/, $options: "si" } }
{ name: { $regex: 'acme.*corp', $options: "si" } }
使用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" }
如下的例子匹配所有文档中sku字段"%789"
db.products.find( { sku: { $regex: /789$/ } } )
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" }
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" }
{ "_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" }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。