日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
MongoDB正則表達式
在編程語言中,正則表達式可以使用單個字符串來描述、匹配一系列符合某個句法規(guī)則的字符串。MongoDB 可以使用 $regex 操作符來設置匹配字符串的正則表達式,MongoDB 使用 PCRE(Perl 兼容的正則表達式)作為正則表達式語言。

創(chuàng)新互聯(lián)建站是一家專業(yè)提供石樓企業(yè)網(wǎng)站建設,專注與成都網(wǎng)站制作、網(wǎng)站設計、成都h5網(wǎng)站建設、小程序制作等業(yè)務。10年已為石樓眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進行中。

與文本搜索不同,您不需要執(zhí)行任何配置或命令就可以直接使用正則表達式。假設我們已經(jīng)在名為 posts 的集合中插入了一個文檔,如下所示:

> db.posts.insert(
... {
...    "post_text": "enjoy the mongodb articles on bianchengbang",
...    "tags": ["mongodb", "bianchengbang"]
... }
... )

使用正則表達式

下面的 regex 查詢將搜索 post_text 字段中包含字符串“bianchengbang”的所有文檔:

> db.posts.find({post_text:{$regex:"bianchengbang"}}).pretty()
{
        "_id" : ObjectId("6041dfc3835e4aa734b591df"),
        "post_text" : "enjoy the mongodb articles on bianchengbang",
        "tags" : [
                "mongodb",
                "bianchengbang"
        ]
}

上面的查詢也可以寫成如下所示的樣子:

> db.posts.find({post_text:/bianchengbang/}).pretty()
{
        "_id" : ObjectId("6041dfc3835e4aa734b591df"),
        "post_text" : "enjoy the mongodb articles on bianchengbang",
        "tags" : [
                "mongodb",
                "bianchengbang"
        ]
}

在不區(qū)分大小寫的情況下使用正則表達式

若要在查詢時不區(qū)分大小寫,我們可以將 $options 參數(shù)的值設置為 $i,下面的命令可以查詢 post_text 字段中包含“bianchengbang”的文檔,且不區(qū)分大小寫。

> db.posts.find({post_text:{$regex:"BianChengBang",$options:"$i"}}).pretty()
{
        "_id" : ObjectId("6041dfc3835e4aa734b591df"),
        "post_text" : "enjoy the mongodb articles on bianchengbang",
        "tags" : [
                "mongodb",
                "bianchengbang"
        ]
}

對數(shù)組元素使用正則表達式

我們還可以在數(shù)組字段上使用正則表達式來查找內(nèi)容,這在實現(xiàn)標簽時非常有用。假如要搜索所有以 b 開頭的字段(例如 bian、biancheng、bianchengbang 等),可以使用如下所示的代碼:

> db.posts.find({tags:{$regex:"b"}}).pretty()
{
        "_id" : ObjectId("6041dfc3835e4aa734b591df"),
        "post_text" : "enjoy the mongodb articles on bianchengbang",
        "tags" : [
                "mongodb",
                "bianchengbang"
        ]
}

優(yōu)化正則表達式查詢

如果文檔字段已建立索引,那么查詢將使用索引值來匹配正則表達式,與使用正則表達式掃描整個集合相比,使用索引來查詢數(shù)據(jù)的速度會更快。

如果正則表達式是前綴表達式,則所有匹配項均應以某個字符串字符開頭。例如,如果正則表達式為 ^tut,則查詢僅需要搜索以 tut 開頭的那些字符串。


標題名稱:MongoDB正則表達式
文章出自:http://www.5511xx.com/article/cojgeoi.html