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


    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" }
    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 
     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 
     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 
     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 
    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" }
    View Code

     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" }
    View Code

    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" }
    View Code
  • 相关阅读:
    Vue + Element 中的时间自定义选择框的数据传参绑定分析与js格式化时间参数
    vue 给data 数据的重新初始化
    Vue + Element 后台项目与后台的数据对接
    js将两组数据存到你定义的空的对象数组
    Element ui 自定义表格行样式
    vue组件的注册与使用
    Vue + Element 实现下拉选择统计时间数据栏并展示
    Vue + Element后台项目报错(This relative module was not found)
    Echart图的使用技巧,配置相关样式与属性
    7种方法实现数组去重
  • 原文地址:https://www.cnblogs.com/qq931399960/p/12906334.html
Copyright © 2020-2023  润新知