MongoDB 正则表达式
正则表达式是使用单个字符串来描述、匹配一系列符合某个句法规则的字符串。
许多程序设计语言都支持利用正则表达式进行字符串操作。
MongoDB 使用 $regex 操作符来设置匹配字符串的正则表达式。
MongoDB使用PCRE (Perl Compatible Regular Expression) 作为正则表达式语言。
不同于全文检索,我们使用正则表达式不需要做任何配置。
考虑以下 posts 集合的文档结构,该文档包含了文章内容和标签:
{
"post_text": "enjoy the mongodb articles on codercto",
"tags": [
"mongodb",
"codercto"
]
}
使用正则表达式
以下命令使用正则表达式查找包含 codercto 字符串的文章:
>db.posts.find({post_text:{$regex:"codercto"}})
以上查询也可以写为:
>db.posts.find({post_text:/codercto/})
不区分大小写的正则表达式
如果检索需要不区分大小写,我们可以设置 $options 为 $i。
以下命令将查找不区分大小写的字符串 codercto:
>db.posts.find({post_text:{$regex:"codercto",$options:"$i"}})
集合中会返回所有包含字符串 codercto 的数据,且不区分大小写:
{
"_id" : ObjectId("53493d37d852429c10000004"),
"post_text" : "hey! this is my post on codercto",
"tags" : [ "codercto" ]
}
数组元素使用正则表达式
我们还可以在数组字段中使用正则表达式来查找内容。 这在标签的实现上非常有用,如果你需要查找包含以 run 开头的标签数据(ru 或 run 或 codercto), 你可以使用以下代码:
>db.posts.find({tags:{$regex:"run"}})
优化正则表达式查询
- 如果你的文档中字段设置了索引,那么使用索引相比于正则表达式匹配查找所有的数据查询速度更快。
- 如果正则表达式是前缀表达式,所有匹配的数据将以指定的前缀字符串为开始。例如: 如果正则表达式为 ^tut ,查询语句将查找以 tut 为开头的字符串。
这里面使用正则表达式有两点需要注意:
正则表达式中使用变量。一定要使用eval将组合的字符串进行转换,不能直接将字符串拼接后传入给表达式。否则没有报错信息,只是结果为空!实例如下:
var name=eval("/" + 变量值key +"/i");
以下是模糊查询包含title关键词, 且不区分大小写:
title:eval("/"+title+"/i") // 等同于 title:{$regex:title,$Option:"$i"}
点击查看所有 MongoDB 教程 文章: https://codercto.com/courses/l/32.html
标签: 正则
Java Servlet & JSP Cookbook
Bruce W. Perry / O'Reilly Media / 2003-12-1 / USD 49.99
With literally hundreds of examples and thousands of lines of code, the Java Servlet and JSP Cookbook yields tips and techniques that any Java web developer who uses JavaServer Pages or servlets will ......一起来看看 《Java Servlet & JSP Cookbook》 这本书的介绍吧!