3分钟掌握MongoDB中的regex几种用法

背景

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参数的含义:



实战
Part1:$in中的用法

要在$in查询中包含正则表达式,只能使用JavaScript正则表达式对象(即/ pattern /)。 例如:

{ name: { 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选项,要求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/可以在匹配前缀后停止扫描。

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的结果。

转载于:https://juejin.im/post/5d020e1c5188256b0d051f7d

猜你喜欢

转载自blog.csdn.net/weixin_33785108/article/details/93181818