這篇文章主要介紹MongoDB增刪改查之查詢?cè)趺磳?shí)現(xiàn),文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
網(wǎng)站設(shè)計(jì)制作過(guò)程拒絕使用模板建站;使用PHP+MYSQL原生開發(fā)可交付網(wǎng)站源代碼;符合網(wǎng)站優(yōu)化排名的后臺(tái)管理系統(tǒng);網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)收費(fèi)合理;免費(fèi)進(jìn)行網(wǎng)站備案等企業(yè)網(wǎng)站建設(shè)一條龍服務(wù).我們是一家持續(xù)穩(wěn)定運(yùn)營(yíng)了10多年的創(chuàng)新互聯(lián)公司網(wǎng)站建設(shè)公司。
>db.t1.find() { "_id" : ObjectId("585ce007d993c80e8713c7bd"), "x" : 1, "j" : 1 } { "_id" : ObjectId("585ce007d993c80e8713c7be"), "x" : 4, "j" : 2 } { "_id" : ObjectId("585ce007d993c80e8713c7bf"), "x" : 2, "j" : 3 } { "_id" : ObjectId("585ce008d993c80e8713c7c0"), "x" : 4, "j" : 4 } { "_id" : ObjectId("585ce023d993c80e8713c7c2"), "x" : 3 }
>db.t1.find().sort({j:-1}) ---"1"表示升序,“-1”表示降序 { "_id" : ObjectId("585ce008d993c80e8713c7c0"), "x" : 4, "j" : 4 } { "_id" : ObjectId("585ce007d993c80e8713c7bf"), "x" : 2, "j" : 3 } { "_id" : ObjectId("585ce007d993c80e8713c7be"), "x" : 4, "j" : 2 } { "_id" : ObjectId("585ce007d993c80e8713c7bd"), "x" : 1, "j" : 1 } { "_id" : ObjectId("585ce023d993c80e8713c7c2"), "x" : 3 }
> db.t1.find({x:2}) { "_id" : ObjectId("585ce007d993c80e8713c7bf"), "x" : 2, "j" : 3 }
> db.t1.find({x:2,j:3}) { "_id" : ObjectId("585ce007d993c80e8713c7bf"), "x" : 2, "j" : 3 }
>db.t1.find({j:{$gt:2,$lte:4}}) ---查詢j字段大于2,小于等于4的記錄 { "_id" : ObjectId("585ce007d993c80e8713c7bf"), "x" : 2, "j" : 3 } { "_id" : ObjectId("585ce008d993c80e8713c7c0"), "x" : 4, "j" : 4 }
比較運(yùn)算符:
$gt:大于
$lt:小于
$gte:大于等于
$lte:小于等于
$ne:不等于
> db.t1.find({j:{$in:[1,4]}}) ---j=1或j=4的記錄,不包括可以用$nin { "_id" : ObjectId("585ce007d993c80e8713c7bd"), "x" : 1, "j" : 1 } { "_id" : ObjectId("585ce008d993c80e8713c7c0"), "x" : 4, "j" : 4 }
> db.t1.find({x:1,j:{$gt:1}}) ---x=1 and j>1條件記錄 { "_id" : ObjectId("585ce007d993c80e8713c7be"), "x" : 1, "j" : 2 } { "_id" : ObjectId("585ce007d993c80e8713c7bf"), "x" : 1, "j" : 3 }
> db.t1.find({$or:[{x:4},{j:{$lte:2}}]}) ---x=4 or j<=2的條件記錄 { "_id" : ObjectId("585ce007d993c80e8713c7bd"), "x" : 1, "j" : 1 } { "_id" : ObjectId("585ce007d993c80e8713c7be"), "x" : 1, "j" : 2 } { "_id" : ObjectId("585ce008d993c80e8713c7c0"), "x" : 4, "j" : 4 }
> db.t1.find({x:1,$or:[{j:{$lt:2}},{j:3}]}) ---x=1 and (j<2 or j=3) { "_id" : ObjectId("585ce007d993c80e8713c7bd"), "x" : 1, "j" : 1 } { "_id" : ObjectId("585ce007d993c80e8713c7bf"), "x" : 1, "j" : 3 }
>db.t6.find({j:{$exists:false}}) ---查詢包含j字段的話,就是$exists:true { "_id" : ObjectId("585ce023d993c80e8713c7c2"), "x" : 3 }
> db.t1.find({x:4},{j:1}) ---默認(rèn)顯示所有字段值,第二個(gè)大括號(hào)使用j:1表示只返回j字段,但主鍵_id默認(rèn)顯示 { "_id" : ObjectId("585ce008d993c80e8713c7c0"), "j" : 4 } { "_id" : ObjectId("585ce007d993c80e8713c7be"), "j" : 2 }
> db.t1.find({x:4},{j:1,_id:0}) { "j" : 4 } { "j" : 2 }
原數(shù)據(jù)表:
> db.t6.find() { "_id" : 1, "x" : 2, "kk" : { "deviceID" : 222, "city" : "Tianjin" } } { "_id" : 2, "x" : 3, "kk" : { "deviceID" : 333, "city" : "Beijing" } } { "_id" : 3, "x" : 2, "kk" : { "deviceID" : 234 } }
查詢嵌套子文檔中的字段,可以用點(diǎn)來(lái)連接外字段與內(nèi)嵌字段,可能不太明白是什么意思,沒關(guān)系,直接看如下舉例即可明白;
> db.t6.find({kk:{deviceID:222,city:"Tianjin"}}) ---查詢kk子文檔中deviceID=222且city:"Tianjin"的記錄 { "_id" : 1, "x" : 2, "kk" : { "deviceID" : 222, "city" : "Tianjin" } }
或者使用如下形式
> db.t6.find({"kk.deviceID":222,"kk.city":"Tianjin"}) ---與上一個(gè)方法一樣的效果,注意雙引號(hào)的使用 { "_id" : 1, "x" : 2, "kk" : { "deviceID" : 222, "city" : "Tianjin" } }
> db.getCollection('t6').find({"kk.city":{$exists:false}}) { "_id" : 3, "x" : 2, "kk" : { "deviceID" : 234 } }
> db.t6.find({},{"kk.deviceID":1,_id:0}) { "kk" : { "deviceID" : 222 } } { "kk" : { "deviceID" : 333 } } { "kk" : { "deviceID" : 234 } }
另:當(dāng)然關(guān)于嵌套子文檔還有更多更復(fù)雜的查詢語(yǔ)句,主要還是看你嵌套文檔中的數(shù)據(jù)結(jié)構(gòu)而定,結(jié)構(gòu)越復(fù)雜,那么需要寫的查詢語(yǔ)句就越復(fù)雜。
MongoDB中數(shù)組的查詢也是比較繁瑣的,因?yàn)楫吘蛊渌P(guān)系型數(shù)據(jù)庫(kù)中不包含此種數(shù)據(jù)類型。不過(guò)沒關(guān)系,我盡量將我能想到的場(chǎng)景羅列于此,方便你我查找。
原數(shù)據(jù)表:
> db.t5.find() { "_id" : 1, "x" : 2, "Course" : [ "English", "Math" ], "score" : [ 1, 3 ] } { "_id" : 2, "x" : 3, "Course" : [ "Math", "English" ], "score" : [ 18, 12 ] } { "_id" : 3, "x" : 4, "Course" : [ "English" ], "score" : [ 98, 1 ] } { "_id" : 4, "x" : 5, "Course" : [ "Yuwen", "English" ], "score" : [ 45, 46 ] } { "_id" : 5, "x" : 7, "Course" : [ "Math" ], "score" : [ 99, 100 ] }
要求:使用{<field>: <value>}形式;
舉例1:匹配一個(gè)元素
> db.t5.find({Course:"Math"}) ---查詢到的是所有包含“Math”元素的行 { "_id" : 1, "x" : 2, "Course" : [ "English", "Math" ], "score" : [ 1, 3 ] } { "_id" : 2, "x" : 3, "Course" : [ "Math", "English" ], "score" : [ 18, 12 ] } { "_id" : 5, "x" : 7, "Course" : [ "Math" ], "score" : [ 99, 100 ] }
舉例2:精確查詢多元素
> db.t5.find({Course:["Math","English"]}) { "_id" : 2, "x" : 3, "Course" : [ "Math", "English" ], "score" : [ 18, 12 ] }
注意: 上述語(yǔ)句只會(huì)查詢到一行,而另外一筆也包含“Math”和“English”的數(shù)據(jù)雖然包含的元素一樣,但是元素順序不一致。所以未被列出
舉例3: 精確查詢一個(gè)元素(按元素位置)
> db.t5.find({"Course.1":"English"}) ---精確查詢數(shù)組中第2個(gè)元素是“English” { "_id" : 2, "x" : 3, "Course" : [ "Math", "English" ], "score" : [ 18, 12 ] } { "_id" : 4, "x" : 5, "Course" : [ "Yuwen", "English" ], "score" : [ 45, 46 ] }
要求:使用$elemMatch操作符;
舉例1:只要求數(shù)組中有一個(gè)元素滿足所有條件
> db.t5.find({score:{$elemMatch:{$gt:15,$lt:20}}}) ---也就只有18>15,且18<20 { "_id" : 2, "x" : 3, "Course" : [ "Math", "English" ], "score" : [ 18, 12 ] }
舉例2: 要求數(shù)組中各個(gè)元素只滿足一部分篩選條件,但是組合起來(lái)可以滿足所有條件
> db.t5.find({score:{$gt:15,$lt:20}}) { "_id" : 2, "x" : 3, "Course" : [ "Math", "English" ], "score" : [ 18, 12 ] } { "_id" : 3, "x" : 4, "Course" : [ "English" ], "score" : [ 98, 1 ] }
另:上述舉例,你可能會(huì)迷糊,為什么[98,1]可以入選呢?因?yàn)?<20,且98>15,所以滿足查詢條件,明白了吧?
>db.t5.find({_id:5}).forEach(function(x){ var score=x.score; var scoreLenth=score.length; print(scoreLenth); }); 2 ---返回結(jié)果
查看數(shù)組不為空:
>db.t9.find({arrary:{$elemMatch:{$ne:null}})
> db.t4.find() { "_id" : ObjectId("58aa8e4c151e4fd0af703688"), "name" : "Jack", "addr" : "tianjin" } { "_id" : ObjectId("58aa8e5e151e4fd0af703689"), "name" : "Tom", "addr" : "beijing" } { "_id" : ObjectId("58aa9c25151e4fd0af70368a"), "name" : "Nick J" }
> db.t4.find({name:/^J/}) ---查詢以J開頭的記錄 { "_id" : ObjectId("58aa8e4c151e4fd0af703688"), "name" : "Jack", "addr" : "tianjin" }
> db.t4.find({name:{$not:/^J/}}) ---查詢不以J開頭的記錄 { "_id" : ObjectId("58aa8e5e151e4fd0af703689"), "name" : "Tom", "addr" : "beijing" } { "_id" : ObjectId("58aa9c25151e4fd0af70368a"), "name" : "Nick J" }
> db.t4.find({name:/J/}) ---查詢name重包含J的記錄 { "_id" : ObjectId("58aa8e4c151e4fd0af703688"), "name" : "Jack", "addr" : "tianjin" } { "_id" : ObjectId("58aa9c25151e4fd0af70368a"), "name" : "Nick J" }
原集合數(shù)據(jù):
> db.t8.find() { "_id" : 99, "name" : null } { "_id" : 100 }
> db.t8.find({name:null}) ---出現(xiàn)這樣的結(jié)果,是因?yàn)檫@樣的查詢方式會(huì)返回name=null且不存在name字段的記錄 { "_id" : 99, "name" : null } { "_id" : 100 }
> db.t8.find({name:{$type:10}}) ---可以使用$type,10其實(shí)就代表了null這個(gè)數(shù)據(jù)類型 { "_id" : 99, "name" : null }
> db.t8.find({name:{$exists:false}}) ---如果只查詢存在name字段的記錄,則$exists:true { "_id" : 100 }
find()查詢之后返回的是一個(gè)cursor游標(biāo),在mongo shell默認(rèn)情況下迭代20次顯示前20個(gè)文檔記錄
如果使用變量迭代結(jié)果集的話:
> var a=db.t6.find() ---定義變量 > while(a.hasNext()){ ---判斷變量游標(biāo)位置 ... printjson(a.next()); ---打印游標(biāo)內(nèi)容 ... } { "_id" : 1, "x" : 2, "kk" : { "deviceID" : 222, "city" : "Tianjin" } } { "_id" : 2, "x" : 2, "kk" : { "deviceID" : 222, "addr" : "Heping" } }
或者使用forEach()
> var a=db.t6.find() > a.forEach(printjson) { "_id" : 1, "x" : 2, "kk" : { "deviceID" : 222, "city" : "Tianjin" } } { "_id" : 2, "x" : 2, "kk" : { "deviceID" : 222, "addr" : "Heping" } }
使用toArry()逐一查詢結(jié)果集
> var a=db.t6.find() ---定義變量 > var doc=a.toArray() ---使用toArray()生成數(shù)組結(jié)果集 > doc[0] ---查詢結(jié)果集中第一個(gè)文檔 { "_id" : 1, "x" : 2, "kk" : { "deviceID" : 222, "city" : "Tianjin" } }
以上是“MongoDB增刪改查之查詢?cè)趺磳?shí)現(xiàn)”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享題目:MongoDB增刪改查之查詢?cè)趺磳?shí)現(xiàn)
標(biāo)題路徑:http://aaarwkj.com/article48/gjoeep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、網(wǎng)頁(yè)設(shè)計(jì)公司、ChatGPT、電子商務(wù)、定制網(wǎng)站、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)