怎么新建MySQL数据库

发布时间:2021-05-15 作者:admin
阅读:575

       我们在使用MongoDB查询时,模糊查询是比较常见用和实用,因此大家要掌握MongoDB模糊查询操作。为了让大家对MongoDB模糊查询有更深的认识,这篇文章就给大家介绍一下关于MongoDB模糊查询的操作,下面就跟随小编一起来看看吧。

       1.作用与语法描述

       作用: 正则表达式是使用指定字符串来描述、匹配一系列符合某个句法规则的字符串。许多程序设计语言都支持利用正则表达式进行字符串操作。MongoDB 使用 $regex 操作符来设置匹配字符串的正则表达式。

       语法一

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

       语法二

{ <field>: /pattern/<options> }

       2.案例演示

       假设MongoDB中存放了我们OrderService服务的消息信息,其数据如下:

       (以下为演示所用到的9个文档)

{
  "_id" : ObjectId("5d305b1c4857fc49c0c14c81"),
  "order" : "QQ00001",
  "data" : "\"<?xml version='1.0' encoding='UTF-8'?><Response service=\\\"OrderService\\\">
  <Head>ERR</Head><ERROR>重复下单</ERROR></Response>\""
},
{
  "_id" : ObjectId("5d305b3b4857fc49c0c14c82"),
  "order" : "QQ00001",
  "data" : "\"<?xml version='1.0' encoding='UTF-8'?><Response service=\\\"OrderService\\\">
  <Head>ERR</Head><ERROR>重复下单</ERROR></Response>\""
},
{
  "_id" : ObjectId("5d305b404857fc49c0c14c83"),
  "order" : "QQ00002",
  "data" : "\"<?xml version='1.0' encoding='UTF-8'?><Response service=\\\"OrderService\\\">
  <Head>ERR</Head><ERROR>重复下单</ERROR></Response>\""
},
{
  "_id" : ObjectId("5d305b454857fc49c0c14c84"),
  "order" : "QQ00002",
  "data" : "\"<?xml version='1.0' encoding='UTF-8'?><Response service=\\\"OrderService\\\">
  <Head>ERR</Head><ERROR>重复下单</ERROR></Response>\""
},
{
  "_id" : ObjectId("5d305b4b4857fc49c0c14c85"),
  "order" : "QQ00003",
  "data" : "\"<?xml version='1.0' encoding='UTF-8'?><Response service=\\\"OrderService\\\">
  <Head>ERR</Head><ERROR>重复下单</ERROR></Response>\""
},
{
  "_id" : ObjectId("5d305b4f4857fc49c0c14c86"),
  "order" : "QQ00003",
  "data" : "\"<?xml version='1.0' encoding='UTF-8'?><Response service=\\\"OrderService\\\">
  <Head>ERR</Head><ERROR>重复下单</ERROR></Response>\""
},
{
  "_id" : ObjectId("5d305bb74857fc49c0c14c87"),
  "order" : "QQ00003",
  "data" : "\"<?xml version='1.0' encoding='UTF-8'?><Response service=\\\"OrderService\\\">
  <Head>OK</Head><Body><OrderResponse filter_mmm=\\\"2\\\" destcity=\\\"shenzheng\\\" origincode=\\\"QQ0003\\\" />
  </Body></Response>\""
},
{
  "_id" : ObjectId("5d305bd14857fc49c0c14c88"),
  "order" : "QQ00002",
  "data" : "\"<?xml version='1.0' encoding='UTF-8'?><Response service=\\\"OrderService\\\">
  <Head>OK</Head><Body><OrderResponse filter_mmm=\\\"2\\\" destcity=\\\"zhongshan\\\" origincode=\\\"QQ0002\\\" />
  </Body></Response>\""
},
{
  "_id" : ObjectId("5d305be94857fc49c0c14c89"),
  "order" : "QQ00001",
  "data" : "\"<?xml version='1.0' encoding='UTF-8'?><Response service=\\\"OrderService\\\">
  <Head>OK</Head><Body><OrderResponse filter_mmm=\\\"2\\\" destcity=\\\"zhongshan\\\" origincode=\\\"QQ0001\\\" />
  </Body></Response>\""
}

       需求: 查询 data 字段 含有 字符 <Head>OK</Head>的文档

       执行代码如下:

db.dbtestregex.find({data:{ $regex:/<Head>OK<\/Head>/}})

       查询代码中的 \ 为转移符,以下查询语句也是如此。

       返回结果如下:

{
  "_id" : ObjectId("5d305bb74857fc49c0c14c87"),
  "order" : "QQ00003",
  "data" : "\"<?xml version='1.0' encoding='UTF-8'?><Response service=\\\"OrderService\\\">
  <Head>OK</Head><Body><OrderResponse filter_mmm=\\\"2\\\" destcity=\\\"shenzheng\\\" origincode=\\\"QQ0003\\\" />
  </Body></Response>\""
},
{
  "_id" : ObjectId("5d305bd14857fc49c0c14c88"),
  "order" : "QQ00002",
  "data" : "\"<?xml version='1.0' encoding='UTF-8'?><Response service=\\\"OrderService\\\">
  <Head>OK</Head><Body><OrderResponse filter_mmm=\\\"2\\\" destcity=\\\"zhongshan\\\" origincode=\\\"QQ0002\\\" />
  </Body></Response>\""
},
{
  "_id" : ObjectId("5d305be94857fc49c0c14c89"),
  "order" : "QQ00001",
  "data" : "\"<?xml version='1.0' encoding='UTF-8'?><Response service=\\\"OrderService\\\">
  <Head>OK</Head><Body><OrderResponse filter_mmm=\\\"2\\\" destcity=\\\"zhongshan\\\" origincode=\\\"QQ0001\\\" />
  </Body></Response>\""
}

       上面的查询命令也可以改写成: 

db.dbtestregex.find({data:/<Head>OK<\/Head>/})

       其查询结果是一样的,如下:

{
  "_id" : ObjectId("5d305bb74857fc49c0c14c87"),
  "order" : "QQ00003",
  "data" : "\"<?xml version='1.0' encoding='UTF-8'?><Response service=\\\"OrderService\\\">
  <Head>OK</Head><Body><OrderResponse filter_mmm=\\\"2\\\" destcity=\\\"shenzheng\\\" origincode=\\\"QQ0003\\\" />
  </Body></Response>\""
},
{
  "_id" : ObjectId("5d305bd14857fc49c0c14c88"),
  "order" : "QQ00002",
  "data" : "\"<?xml version='1.0' encoding='UTF-8'?><Response service=\\\"OrderService\\\">
  <Head>OK</Head><Body><OrderResponse filter_mmm=\\\"2\\\" destcity=\\\"zhongshan\\\" origincode=\\\"QQ0002\\\" />
  </Body></Response>\""
},
{
  "_id" : ObjectId("5d305be94857fc49c0c14c89"),
  "order" : "QQ00001",
  "data" : "\"<?xml version='1.0' encoding='UTF-8'?><Response service=\\\"OrderService\\\">
  <Head>OK</Head><Body><OrderResponse filter_mmm=\\\"2\\\" destcity=\\\"zhongshan\\\" origincode=\\\"QQ0001\\\" />
  </Body></Response>\""
}

        还可以写成:

       db.dbtestregex.find({data:{ $regex:'<Head>OK<\/Head>'}})

       其实现功能一样。

       3.类关系型数据库中的 not like 功能实现

       反向查询虽然不常见,但也是我们DBA必须的。下面我们做一个案例演示,看看在MongoDB中,怎么实现not like 功能的查询。

       需求 查询data 字段中不含  <Head>OK</Head>的文档

       执行脚本如下:

 db.dbtestregex.find({data:{ $not: /<Head>OK<\/Head>/ }}) 

       返回的文档如下;

{
  "_id" : ObjectId("5d305b1c4857fc49c0c14c81"),
  "order" : "QQ00001",
  "data" : "\"<?xml version='1.0' encoding='UTF-8'?><Response service=\\\"OrderService\\\">
  <Head>ERR</Head><ERROR>重复下单</ERROR></Response>\""
},
{
  "_id" : ObjectId("5d305b3b4857fc49c0c14c82"),
  "order" : "QQ00001",
  "data" : "\"<?xml version='1.0' encoding='UTF-8'?><Response service=\\\"OrderService\\\">
  <Head>ERR</Head><ERROR>重复下单</ERROR></Response>\""
},
{
  "_id" : ObjectId("5d305b404857fc49c0c14c83"),
  "order" : "QQ00002",
  "data" : "\"<?xml version='1.0' encoding='UTF-8'?><Response service=\\\"OrderService\\\">
  <Head>ERR</Head><ERROR>重复下单</ERROR></Response>\""
},
{
  "_id" : ObjectId("5d305b454857fc49c0c14c84"),
  "order" : "QQ00002",
  "data" : "\"<?xml version='1.0' encoding='UTF-8'?><Response service=\\\"OrderService\\\">
  <Head>ERR</Head><ERROR>重复下单</ERROR></Response>\""
},
{
  "_id" : ObjectId("5d305b4b4857fc49c0c14c85"),
  "order" : "QQ00003",
  "data" : "\"<?xml version='1.0' encoding='UTF-8'?><Response service=\\\"OrderService\\\">
  <Head>ERR</Head><ERROR>重复下单</ERROR></Response>\""
},
{
  "_id" : ObjectId("5d305b4f4857fc49c0c14c86"),
  "order" : "QQ00003",
  "data" : "\"<?xml version='1.0' encoding='UTF-8'?><Response service=\\\"OrderService\\\">
  <Head>ERR</Head><ERROR>重复下单</ERROR></Response>\""
}

       总结

       关于MongoDB模糊查询的操作就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。想要看更多MongoDB模糊查询的内容,可以关注其他相关文章。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

二维码-群英

长按识别二维码并关注微信

更方便到期提醒、手机管理

7*24 全天候服务

售前 400-678-4567

售后 0668-2555666

售后 400 678 4567

信息安全 0668-2555 118

域名空间 3004329145