標籤:mongodb 彙總架構
使用彙總架構可以對集合中的文檔進行變換和組合。用多個構件建立一個管道(pipeline),用於對一連串的文檔進行處理。這些構件包括:
篩選(filtering)
投射(projecting)
分組(grouping)
排序(sorting)
限制(limiting)
跳過(skipping)
在MongoDB中實際的集合架構,需要將這些操作傳給aggregate函數,例如:
(1)將文檔的name欄位投射出來
> 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欄位的值對其分組並計算其個數
> db.post.aggregate({$group:{"_id":"$name","count":{$sum:1}}}) { "_id" : null, "count" : 13 } { "_id" : "joe", "count" : 2 } >
(3)對結果集進行降序排列
> 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個結果
> 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 } >
彙總架構類似關係行資料庫中的select,不會對集合進行寫入操作,結果只會返回給用戶端。
本文出自 “緣隨心愿” 部落格,請務必保留此出處http://281816327.blog.51cto.com/907015/1604102
【MongoDB學習筆記30】MongoDB的彙總架構