当前位置:Gxlcms > 数据库问题 > mongodb正则$regex命令行简单使用

mongodb正则$regex命令行简单使用

时间:2021-07-01 10:21:17 帮助过:17人阅读

正则能帮助我们实现一些复杂的查询,mongodb中实现正也很简单

https://docs.mongodb.com/manual/reference/operator/query/regex/index.html

查询格式

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

options可选值

i:不区分大小写

m:如果字符串中包含\n,m会将\n后面的字符也当成一行处理,这对一个字符串中存在多行的情况比较有用

x:忽略字符串中的注释和换行(注释以#开头)?

s:允许点匹配所有字符?

命令行操作

测试数据

> db.user_info.insertMany([{"name":"liuqingyun"},{"name":"jeams bean"},{"name":"liuqing"},{"name":"Doc veren"},{"name":"doc jan"},{"name":"yuhuanhuan"},{"name":"liu #ming \n ming"},{"name":"doc liu"},{"name":"DOC jeson"}])

1、查询name以包含liu和yu名称

db.user_info.find({"name":{"$in":[/liu/,/yu/]}})
技术图片
> db.user_info.find({"name":{"$in":[/liu/,/yu/]}})
{ "_id" : ObjectId("5ec101d873e561131511bf15"), "name" : "liuqingyun" }
{ "_id" : ObjectId("5ec101d873e561131511bf17"), "name" : "liuqing" }
{ "_id" : ObjectId("5ec101d873e561131511bf1a"), "name" : "yuhuanhuan" }
{ "_id" : ObjectId("5ec101d873e561131511bf1b"), "name" : "liu #ming \n ming" }
{ "_id" : ObjectId("5ec101d873e561131511bf1c"), "name" : "doc liu" }
View Code

2、查询name以liu和yu开头的名称

db.user_info.find({"name":{"$in":[/^liu/,/^yu/]}})
技术图片
> db.user_info.find({"name":{"$in":[/^liu/,/^yu/]}})
{ "_id" : ObjectId("5ec101d873e561131511bf15"), "name" : "liuqingyun" }
{ "_id" : ObjectId("5ec101d873e561131511bf17"), "name" : "liuqing" }
{ "_id" : ObjectId("5ec101d873e561131511bf1a"), "name" : "yuhuanhuan" }
{ "_id" : ObjectId("5ec101d873e561131511bf1b"), "name" : "liu #ming \n ming" }
View Code

3、查询name不包含liu和yu字符的名称

db.user_info.find({"name":{"$nin":[/liu/,/yu/]}})
技术图片
> db.user_info.find({"name":{"$nin":[/liu/,/yu/]}})
{ "_id" : ObjectId("5ec1037873e561131511bf26"), "name" : "jeams bean" }
{ "_id" : ObjectId("5ec1037873e561131511bf28"), "name" : "Doc veren" }
{ "_id" : ObjectId("5ec1037873e561131511bf29"), "name" : "doc jan" }
{ "_id" : ObjectId("5ec1037873e561131511bf2d"), "name" : "DOC jeson" }
View Code

4、使用$not查询name不包含liu的字符

db.user_info.find({"name":{"$not":/liu/}})
技术图片
> db.user_info.find({"name":{"$not":/liu/}})
{ "_id" : ObjectId("5ec1037873e561131511bf26"), "name" : "jeams bean" }
{ "_id" : ObjectId("5ec1037873e561131511bf28"), "name" : "Doc veren" }
{ "_id" : ObjectId("5ec1037873e561131511bf29"), "name" : "doc jan" }
{ "_id" : ObjectId("5ec1037873e561131511bf2a"), "name" : "yuhuanhuan" }
{ "_id" : ObjectId("5ec1037873e561131511bf2d"), "name" : "DOC jeson" }
View Code

或者使用

db.user_info.find({"name":{"$not":{"$regex":/liu/}}})
技术图片
> db.user_info.find({"name":{"$not":{"$regex":/liu/}}})
{ "_id" : ObjectId("5ec1037873e561131511bf26"), "name" : "jeams bean" }
{ "_id" : ObjectId("5ec1037873e561131511bf28"), "name" : "Doc veren" }
{ "_id" : ObjectId("5ec1037873e561131511bf29"), "name" : "doc jan" }
{ "_id" : ObjectId("5ec1037873e561131511bf2a"), "name" : "yuhuanhuan" }
{ "_id" : ObjectId("5ec1037873e561131511bf2d"), "name" : "DOC jeson" }
View Code

或者使用

db.user_info.find({"name":{"$not":{"$regex":"liu"}}}
技术图片
> db.user_info.find({"name":{"$not":{"$regex":"liu"}}})
{ "_id" : ObjectId("5ec1037873e561131511bf26"), "name" : "jeams bean" }
{ "_id" : ObjectId("5ec1037873e561131511bf28"), "name" : "Doc veren" }
{ "_id" : ObjectId("5ec1037873e561131511bf29"), "name" : "doc jan" }
{ "_id" : ObjectId("5ec1037873e561131511bf2a"), "name" : "yuhuanhuan" }
{ "_id" : ObjectId("5ec1037873e561131511bf2d"), "name" : "DOC jeson" }
View Code

5、使用$not查询不以liu开头的字符

db.user_info.find({"name":{"$not":/^liu/}})
技术图片
> db.user_info.find({"name":{"$not":/^liu/}})
{ "_id" : ObjectId("5ec1037873e561131511bf26"), "name" : "jeams bean" }
{ "_id" : ObjectId("5ec1037873e561131511bf28"), "name" : "Doc veren" }
{ "_id" : ObjectId("5ec1037873e561131511bf29"), "name" : "doc jan" }
{ "_id" : ObjectId("5ec1037873e561131511bf2a"), "name" : "yuhuanhuan" }
{ "_id" : ObjectId("5ec1037873e561131511bf2c"), "name" : "doc liu" }
{ "_id" : ObjectId("5ec1037873e561131511bf2d"), "name" : "DOC jeson" }
View Code

或者使用

db.user_info.find({"name":{"$not":{"$regex":/^liu/}}})
技术图片
> db.user_info.find({"name":{"$not":{"$regex":/^liu/}}})
{ "_id" : ObjectId("5ec1037873e561131511bf26"), "name" : "jeams bean" }
{ "_id" : ObjectId("5ec1037873e561131511bf28"), "name" : "Doc veren" }
{ "_id" : ObjectId("5ec1037873e561131511bf29"), "name" : "doc jan" }
{ "_id" : ObjectId("5ec1037873e561131511bf2a"), "name" : "yuhuanhuan" }
{ "_id" : ObjectId("5ec1037873e561131511bf2c"), "name" : "doc liu" }
{ "_id" : ObjectId("5ec1037873e561131511bf2d"), "name" : "DOC jeson" }
View Code

或者使用

db.user_info.find({"name":{"$not":{"$regex":"^liu"}}})
技术图片
> db.user_info.find({"name":{"$not":{"$regex":"^liu"}}})
{ "_id" : ObjectId("5ec1037873e561131511bf26"), "name" : "jeams bean" }
{ "_id" : ObjectId("5ec1037873e561131511bf28"), "name" : "Doc veren" }
{ "_id" : ObjectId("5ec1037873e561131511bf29"), "name" : "doc jan" }
{ "_id" : ObjectId("5ec1037873e561131511bf2a"), "name" : "yuhuanhuan" }
{ "_id" : ObjectId("5ec1037873e561131511bf2c"), "name" : "doc liu" }
{ "_id" : ObjectId("5ec1037873e561131511bf2d"), "name" : "DOC jeson" }
View Code

经测试,mongodb的正则表达式中/可以用“代替,这样在java代码调用时也比较容易,如下使用“代替/测试

6、查询name中包含liu的名称

db.user_info.find({"name":{"$regex":"liu"}})
技术图片
> db.user_info.find({"name":{"$regex":"liu"}})
{ "_id" : ObjectId("5ec1037873e561131511bf25"), "name" : "liuqingyun" }
{ "_id" : ObjectId("5ec1037873e561131511bf27"), "name" : "liuqing" }
{ "_id" : ObjectId("5ec1037873e561131511bf2b"), "name" : "liu #ming \n ming" }
{ "_id" : ObjectId("5ec1037873e561131511bf2c"), "name" : "doc liu" }
View Code

7、查询name中以liu开头的名称

db.user_info.find({"name":{"$regex":"^liu"}})
技术图片
> db.user_info.find({"name":{"$regex":"^liu"}})
{ "_id" : ObjectId("5ec1037873e561131511bf25"), "name" : "liuqingyun" }
{ "_id" : ObjectId("5ec1037873e561131511bf27"), "name" : "liuqing" }
{ "_id" : ObjectId("5ec1037873e561131511bf2b"), "name" : "liu #ming \n ming" }
View Code

8、查询name以liu结尾的名称

db.user_info.find({"name":{"$regex":"liu$"}})
技术图片
> db.user_info.find({"name":{"$regex":"liu$"}})
{ "_id" : ObjectId("5ec1037873e561131511bf2c"), "name" : "doc liu" }
View Code

9、查询包含yu,紧跟着两个huan字符串的名称

db.user_info.find({"name":{"$regex":"yu(huan){2}"}})
技术图片
> db.user_info.find({"name":{"$regex":"yu(huan){2}"}})
{ "_id" : ObjectId("5ec1037873e561131511bf2a"), "name" : "yuhuanhuan" }
View Code

10、查询name以l开头,g结尾的名称

db.user_info.find({"name":{"$regex":"^l.*g$"}})
技术图片
> db.user_info.find({"name":{"$regex":"^l.*g$"}})
{ "_id" : ObjectId("5ec1037873e561131511bf27"), "name" : "liuqing" }
View Code

11、查询以doc开头的名称,不区分大小写,mongodb正则默认区分大小写

db.user_info.find({"name":{"$regex":"(?i)^doc"}})
技术图片
> db.user_info.find({"name":{"$regex":"(?i)^doc"}})
{ "_id" : ObjectId("5ec1037873e561131511bf28"), "name" : "Doc veren" }
{ "_id" : ObjectId("5ec1037873e561131511bf29"), "name" : "doc jan" }
{ "_id" : ObjectId("5ec1037873e561131511bf2c"), "name" : "doc liu" }
{ "_id" : ObjectId("5ec1037873e561131511bf2d"), "name" : "DOC jeson" }
View Code

或者

db.user_info.find({"name":{"$regex":/^doc/i}})

12、匹配以jeams开头的字符串(正则m字符使用)

在之前数据基础上插入如下数据

db.user_info.insertOne({"name":"ming #wang \njeams jan"})
> db.user_info.find({"name":{"$regex":"^jeams"}})
{ "_id" : ObjectId("5ec1037873e561131511bf26"), "name" : "jeams bean" 

该命令只返回一行数据

> db.user_info.find({"name":{"$regex":"(?m)^jeams"}})
{ "_id" : ObjectId("5ec1037873e561131511bf26"), "name" : "jeams bean" }
{ "_id" : ObjectId("5ec10f7e73e561131511bf2f"), "name" : "ming #wang \njeams jan" }

加上正则字符m,返回两行数据,m将\n当成换行符处理。而不是一个普通的字符

13、使用点配置所有字符

文档中讲,使用s可以配置包括换行符在内的所有字符,但是不使用s,似乎也是可以配置到所有字符?

> db.user_info.find({"name":{"$regex":"liu.*g"}})
{ "_id" : ObjectId("5ec1037873e561131511bf25"), "name" : "liuqingyun" }
{ "_id" : ObjectId("5ec1037873e561131511bf27"), "name" : "liuqing" }
{ "_id" : ObjectId("5ec1037873e561131511bf2b"), "name" : "liu #ming \n ming" }
> 
> db.user_info.find({"name":{"$regex":"(?s)liu.*g"}})
{ "_id" : ObjectId("5ec1037873e561131511bf25"), "name" : "liuqingyun" }
{ "_id" : ObjectId("5ec1037873e561131511bf27"), "name" : "liuqing" }
{ "_id" : ObjectId("5ec1037873e561131511bf2b"), "name" : "liu #ming \n ming" }

14、查询name以liu开头,后面跟着4个字符的名称

 db.user_info.find({"name":{"$regex":"^liu.{4}$"}})
技术图片
> db.user_info.find({"name":{"$regex":"^liu.{4}$"}})
{ "_id" : ObjectId("5ec1037873e561131511bf27"), "name" : "liuqing" }
View Code

 15、查询以doc开头全部忽略大小写的名称

db.user_info.find({"name
                        
                    

人气教程排行