您好,登錄后才能下訂單哦!
這篇文章主要介紹了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模糊查詢操作的示例分析”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。