使用聚合框架可以對(duì)集合中的文檔進(jìn)行變換和組合。用多個(gè)構(gòu)件創(chuàng)建一個(gè)管道(pipeline),用于對(duì)一連串的文檔進(jìn)行處理。這些構(gòu)件包括:
為臨江等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及臨江網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都做網(wǎng)站、網(wǎng)站建設(shè)、臨江網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!篩選(filtering)
投射(projecting)
分組(grouping)
排序(sorting)
限制(limiting)
跳過(skipping)
在MongoDB中實(shí)際的集合框架,需要將這些操作傳給aggregate函數(shù),例如:
(1)將文檔的name字段投射出來(lái)
> db.post.aggregate({$project:{"name":1}}) { "_id" : ObjectId("54a530c3ff0df3732bac1681"), "name" : "joe" } { "_id" : ObjectId("54a530c3ff0df3732bac1680"), "name" : "joe" } { "_id" : ObjectId("54a9700e1b5afd45354fd086") } { "_id" : ObjectId("54a9701c1b5afd45354fd087") } { "_id" : ObjectId("54a970281b5afd45354fd088") } { "_id" : ObjectId("54a970351b5afd45354fd089") } { "_id" : ObjectId("54a970781b5afd45354fd08a") } { "_id" : ObjectId("54a970831b5afd45354fd08b") } { "_id" : ObjectId("54a970901b5afd45354fd08c") } { "_id" : ObjectId("54a9709c1b5afd45354fd08d") } { "_id" : ObjectId("54aa8a90652d8bdfa0566d34") } { "_id" : ObjectId("54aa97b894dcf31069b590ca") } { "_id" : ObjectId("54aa97d794dcf31069b590cb") } { "_id" : ObjectId("54aa97f294dcf31069b590cc") } { "_id" : ObjectId("54aff7f43bd1048e7b585e39") } > db.post.aggregate({$project:{"name":1,"_id":0}}) { "name" : "joe" } { "name" : "joe" }可以看的出了find的用法類似;
(2)以name字段的值對(duì)其分組并計(jì)算其個(gè)數(shù)
> db.post.aggregate({$group:{"_id":"$name","count":{$sum:1}}}) { "_id" : null, "count" : 13 } { "_id" : "joe", "count" : 2 } >(3)對(duì)結(jié)果集進(jìn)行降序排列
> db.post.aggregate({$sort:{"id":-1}}) { "_id" : ObjectId("54aa97d794dcf31069b590cb"), "id" : 13, "fruit" : [ "apple", "kumquat", "orange", "fruit01" ] } { "_id" : ObjectId("54aa97b894dcf31069b590ca"), "id" : 12, "fruit" : [ "apple", "banana", "peach" ] } { "_id" : ObjectId("54aa8a90652d8bdfa0566d34"), "id" : 11, "test10" : 11 } { "_id" : ObjectId("54a9709c1b5afd45354fd08d"), "id" : 10, "test10" : 10 } { "_id" : ObjectId("54a970901b5afd45354fd08c"), "id" : 9, "test9" : 9 } { "_id" : ObjectId("54a970831b5afd45354fd08b"), "id" : 8, "test8" : 8 } { "_id" : ObjectId("54a970781b5afd45354fd08a"), "id" : 7, "test7" : 7 } { "_id" : ObjectId("54a970351b5afd45354fd089"), "id" : 6, "test6" : 6 } { "_id" : ObjectId("54a970281b5afd45354fd088"), "id" : 5, "test5" : 5 } { "_id" : ObjectId("54a9701c1b5afd45354fd087"), "id" : 4, "test4" : 4 } { "_id" : ObjectId("54a9700e1b5afd45354fd086"), "id" : 3, "test3" : 3 } { "_id" : ObjectId("54a530c3ff0df3732bac1681"), "id" : 2, "name" : "joe", "age" : 30, "sex" : 1, "school" : "marry" } { "_id" : ObjectId("54a530c3ff0df3732bac1680"), "id" : 1, "name" : "joe", "age" : 30, "comments" : [ "test2", "test9", "test5" ], "sex" : 1, "school" : "marry" } { "_id" : ObjectId("54aa97f294dcf31069b590cc"), "push" : { "fruit" : "fruit01" } } { "_id" : ObjectId("54aff7f43bd1048e7b585e39"), "username" : "sid", "loc" : { "ip" : "1.2.3.4", "city" : "springfield", "state" : "ny" } } >
(4)限制顯示排序后的前5個(gè)結(jié)果
> db.post.aggregate({$sort:{"id":-1}},{$limit:5}) { "_id" : ObjectId("54aa97d794dcf31069b590cb"), "id" : 13, "fruit" : [ "apple", "kumquat", "orange", "fruit01" ] } { "_id" : ObjectId("54aa97b894dcf31069b590ca"), "id" : 12, "fruit" : [ "apple", "banana", "peach" ] } { "_id" : ObjectId("54aa8a90652d8bdfa0566d34"), "id" : 11, "test10" : 11 } { "_id" : ObjectId("54a9709c1b5afd45354fd08d"), "id" : 10, "test10" : 10 } { "_id" : ObjectId("54a970901b5afd45354fd08c"), "id" : 9, "test9" : 9 } >
聚合框架類似關(guān)系行數(shù)據(jù)庫(kù)中的select,不會(huì)對(duì)集合進(jìn)行寫入操作,結(jié)果只會(huì)返回給客戶端。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
新聞標(biāo)題:【MongoDB學(xué)習(xí)筆記30】MongoDB的聚合框架-創(chuàng)新互聯(lián)
轉(zhuǎn)載來(lái)源:http://aaarwkj.com/article32/cojisc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、企業(yè)網(wǎng)站制作、軟件開發(fā)、營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、網(wǎng)站導(dǎo)航
聲明:本網(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)
猜你還喜歡下面的內(nèi)容