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)系