加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 服务器 > 系统 > 正文

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

发布时间:2023-09-28 15:26:30 所属栏目:系统 来源:
导读:Part1:写在最前

使用MySQL或其他关系型数据库的朋友们都知道,使用模糊查询的用法类似于:

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

本文中介绍的MongoDB中的regex就是实现类似功能的,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参数的含义:
 
选项 含义 使用要求
 
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无法使用不区分大小写的索引。
 
一个商品的集合中,存了以下内容
 
{ "_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的结果。
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章