Mongodb正则$regex
正则能帮助我们实现一些复杂的查询,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:如果字符串中包含 ,m会将 后面的字符也当成一行处理,这对一个字符串中存在多行的情况比较有用
x:忽略字符串中的注释和换行(注释以#开头)?
s:允许点匹配所有字符?
命令行操作
测试数据
> db.user_info.insertMany([{"name":"liuqingyun"},{"name":"jeams bean"},{"name":"liuqing"},{"name":"Doc veren"},{"name":"doc jan"},{"name":"yuhuanhuan"},{"name":"liu #ming 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 ming" } { "_id" : ObjectId("5ec101d873e561131511bf1c"), "name" : "doc liu" }
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 ming" }
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" }
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" }
或者使用
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" }
或者使用
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" }
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" }
或者使用
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" }
或者使用
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" }
经测试,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 ming" } { "_id" : ObjectId("5ec1037873e561131511bf2c"), "name" : "doc liu" }
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 ming" }
8、查询name以liu结尾的名称
db.user_info.find({"name":{"$regex":"liu$"}})
> db.user_info.find({"name":{"$regex":"liu$"}}) { "_id" : ObjectId("5ec1037873e561131511bf2c"), "name" : "doc liu" }
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" }
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" }
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" }
或者
db.user_info.find({"name":{"$regex":/^doc/i}})
12、匹配以jeams开头的字符串(正则m字符使用)
在之前数据基础上插入如下数据
db.user_info.insertOne({"name":"ming #wang jeams 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 jeams jan" }
加上正则字符m,返回两行数据,m将 当成换行符处理。而不是一个普通的字符
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 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 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" }
15、查询以doc开头全部忽略大小写的名称
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" }
16、查询以D开头,其他字符忽略大小写的名称
db.user_info.find({"name":{"$regex":"^D(?i)oc"}})
> db.user_info.find({"name":{"$regex":"^D(?i)oc"}}) { "_id" : ObjectId("5ec1037873e561131511bf28"), "name" : "Doc veren" } { "_id" : ObjectId("5ec1037873e561131511bf2d"), "name" : "DOC jeson" }