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