主頁(yè) > 知識(shí)庫(kù) > MongoDB模糊查詢操作案例詳解(類關(guān)系型數(shù)據(jù)庫(kù)的 like 和 not like)

MongoDB模糊查詢操作案例詳解(類關(guān)系型數(shù)據(jù)庫(kù)的 like 和 not like)

熱門標(biāo)簽:內(nèi)蒙古營(yíng)銷智能外呼系統(tǒng)哪個(gè)好 crm外呼系統(tǒng)聯(lián)系方式 呼和浩特外呼系統(tǒng)原理是什么 青白江400企業(yè)電話申請(qǐng) 智能外呼系統(tǒng)官網(wǎng) 小裙科技電銷機(jī)器人怎樣 河南電話外呼系統(tǒng)招商 長(zhǎng)沙電銷外呼防封卡是什么 外呼線路資源屬于電信業(yè)務(wù)嗎

1.作用與語(yǔ)法描述

作用: 正則表達(dá)式是使用指定字符串來(lái)描述、匹配一系列符合某個(gè)句法規(guī)則的字符串。許多程序設(shè)計(jì)語(yǔ)言都支持利用正則表達(dá)式進(jìn)行字符串操作。MongoDB 使用 $regex 操作符來(lái)設(shè)置匹配字符串的正則表達(dá)式。

語(yǔ)法一

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

語(yǔ)法二

{ field>: /pattern/options> }

2.案例演示

假設(shè)MongoDB中存放了我們OrderService服務(wù)的消息信息,其數(shù)據(jù)如下:

(以下為演示所用到的9個(gè)文檔)

{
  "_id" : ObjectId("5d305b1c4857fc49c0c14c81"),
  "order" : "QQ00001",
  "data" : "\"?xml version='1.0' encoding='UTF-8'?>Response service=\\\"OrderService\\\">Head>ERR/Head>ERROR>重復(fù)下單/ERROR>/Response>\""
},
{
  "_id" : ObjectId("5d305b3b4857fc49c0c14c82"),
  "order" : "QQ00001",
  "data" : "\"?xml version='1.0' encoding='UTF-8'?>Response service=\\\"OrderService\\\">Head>ERR/Head>ERROR>重復(fù)下單/ERROR>/Response>\""
},
{
  "_id" : ObjectId("5d305b404857fc49c0c14c83"),
  "order" : "QQ00002",
  "data" : "\"?xml version='1.0' encoding='UTF-8'?>Response service=\\\"OrderService\\\">Head>ERR/Head>ERROR>重復(fù)下單/ERROR>/Response>\""
},
{
  "_id" : ObjectId("5d305b454857fc49c0c14c84"),
  "order" : "QQ00002",
  "data" : "\"?xml version='1.0' encoding='UTF-8'?>Response service=\\\"OrderService\\\">Head>ERR/Head>ERROR>重復(fù)下單/ERROR>/Response>\""
},
{
  "_id" : ObjectId("5d305b4b4857fc49c0c14c85"),
  "order" : "QQ00003",
  "data" : "\"?xml version='1.0' encoding='UTF-8'?>Response service=\\\"OrderService\\\">Head>ERR/Head>ERROR>重復(fù)下單/ERROR>/Response>\""
},
{
  "_id" : ObjectId("5d305b4f4857fc49c0c14c86"),
  "order" : "QQ00003",
  "data" : "\"?xml version='1.0' encoding='UTF-8'?>Response service=\\\"OrderService\\\">Head>ERR/Head>ERROR>重復(fù)下單/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>的文檔

執(zhí)行代碼如下:

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

查詢代碼中的 \ 為轉(zhuǎn)移符,以下查詢語(yǔ)句也是如此。

返回結(jié)果如下:

{
  "_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>/})

其查詢結(jié)果是一樣的,如下:

{
  "_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>'}})

其實(shí)現(xiàn)功能一樣。

3.類關(guān)系型數(shù)據(jù)庫(kù)中的 not like 功能實(shí)現(xiàn)

反向查詢雖然不常見(jiàn),但也是我們DBA必須的。下面我們做一個(gè)案例演示,看看在MongoDB中,怎么實(shí)現(xiàn)not like 功能的查詢。

需求 查詢data 字段中不含  Head>OK/Head>的文檔

執(zhí)行腳本如下:

 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>重復(fù)下單/ERROR>/Response>\""
},
{
  "_id" : ObjectId("5d305b3b4857fc49c0c14c82"),
  "order" : "QQ00001",
  "data" : "\"?xml version='1.0' encoding='UTF-8'?>Response service=\\\"OrderService\\\">Head>ERR/Head>ERROR>重復(fù)下單/ERROR>/Response>\""
},
{
  "_id" : ObjectId("5d305b404857fc49c0c14c83"),
  "order" : "QQ00002",
  "data" : "\"?xml version='1.0' encoding='UTF-8'?>Response service=\\\"OrderService\\\">Head>ERR/Head>ERROR>重復(fù)下單/ERROR>/Response>\""
},
{
  "_id" : ObjectId("5d305b454857fc49c0c14c84"),
  "order" : "QQ00002",
  "data" : "\"?xml version='1.0' encoding='UTF-8'?>Response service=\\\"OrderService\\\">Head>ERR/Head>ERROR>重復(fù)下單/ERROR>/Response>\""
},
{
  "_id" : ObjectId("5d305b4b4857fc49c0c14c85"),
  "order" : "QQ00003",
  "data" : "\"?xml version='1.0' encoding='UTF-8'?>Response service=\\\"OrderService\\\">Head>ERR/Head>ERROR>重復(fù)下單/ERROR>/Response>\""
},
{
  "_id" : ObjectId("5d305b4f4857fc49c0c14c86"),
  "order" : "QQ00003",
  "data" : "\"?xml version='1.0' encoding='UTF-8'?>Response service=\\\"OrderService\\\">Head>ERR/Head>ERROR>重復(fù)下單/ERROR>/Response>\""
}

更多內(nèi)容可參照官方文檔  https://docs.mongodb.com/manual/reference/operator/query/regex/#op._S_regex

總結(jié)

以上所述是小編給大家介紹的MongoDB模糊查詢操作案例詳解(類關(guān)系型數(shù)據(jù)庫(kù)的 like 和 not like),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

您可能感興趣的文章:
  • mongodb與sql關(guān)系型數(shù)據(jù)比較
  • MongoDB學(xué)習(xí)筆記(四) 用MongoDB的文檔結(jié)構(gòu)描述數(shù)據(jù)關(guān)系
  • 淺談MongoDB 關(guān)系

標(biāo)簽:白山 呼倫貝爾 安順 菏澤 舟山 黃石 楚雄 池州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MongoDB模糊查詢操作案例詳解(類關(guān)系型數(shù)據(jù)庫(kù)的 like 和 not like)》,本文關(guān)鍵詞  MongoDB,模糊,查詢,操作,案例,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MongoDB模糊查詢操作案例詳解(類關(guān)系型數(shù)據(jù)庫(kù)的 like 和 not like)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于MongoDB模糊查詢操作案例詳解(類關(guān)系型數(shù)據(jù)庫(kù)的 like 和 not like)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章