MongoDB的模糊查询操作(类关系型数据库的 like 和 not like)
程序员文章站
2022-06-24 11:06:37
1.作用与语法描述 作用: 正则表达式是使用指定字符串来描述、匹配一系列符合某个句法规则的字符串。许多程序设计语言都支持利用正则表达式进行字符串操作。MongoDB 使用 $regex 操作符来设置匹配字符串的正则表达式。 语法一 语法二 2.案例演示 假设MongoDB中存放了我们OrderSer ......
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>\"" }
更多内容可参照官方文档 https://docs.mongodb.com/manual/reference/operator/query/regex/#op._s_regex