【MongoDB学习笔记30】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,不会对集合进行写入操作,结果只会返回给客户端。
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
名称栏目:【MongoDB学习笔记30】MongoDB的聚合框架-创新互联
标题网址:http://cdiso.cn/article/cojisc.html