Chinaunix首页 | 论坛 | 博客
  • 博客访问: 227590
  • 博文数量: 48
  • 博客积分: 1088
  • 博客等级: 少尉
  • 技术积分: 339
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-27 10:24
文章分类

全部博文(48)

文章存档

2012年(48)

我的朋友

分类: LINUX

2012-11-21 14:47:33

2.2.0新加的一个很好用的框架
两个概念pipeline类似于linux shell中的管道操作,用于对文档进行过滤和修改; 
                  expression用于对pipeline指令进行逻辑运算
如何使用?db.people.aggregate( [] )

pipeline介绍:
原始文档: { title : "this is my title" , author : "bob" , posted : new Date() , pageViews : 5 , tags : [ "fun" , "good" , "fun" ] , comments : [ { author :"joe" , text : "this is cool" } , { author :"sam" , text : "this is bad" } ], other : { foo : 5 }}
1.$project 可以理解成表列的投影操作,可以增加、删除、重命名和定义子对象作为返回列(尤其这个比较NB!)
   i.e. db.article.aggregate(
{ $project : { title : 1 , stats : { pv : "$pageViews", foo : "$other.foo", dpv : { $add:["$pageViews", 10] } } }});
2. $match 相当于一个query,将这个pipeline放在开始处能有效过滤文档,并能使用索引
  i.e. db.article.aggregate(
{ $match : { score : { $gt : 50, $lte : 90 } } });
3. $limit $skip 不多解释

4.$unwind 将数组中的元素进行分裂,得到的文档数=数组元素数,这些文档只有指定列不同
  i.e. db.article.aggregate(
{ $project : { author : 1 , title : 1 , tags : 1 }}, { $unwind : "$tags" }) { "result" : [ { "_id" : ObjectId("4e6e4ef557b77501a49233f6"), "title" : "this is my title", "author" : "bob", "tags" : "fun" }, { "_id" : ObjectId("4e6e4ef557b77501a49233f6"), "title" : "this is my title", "author" : "bob", "tags" : "good" }, { "_id" : ObjectId("4e6e4ef557b77501a49233f6"), "title" : "this is my title", "author" : "bob", "tags" : "fun" } ], "OK" : 1}
5.$group 吃内存
   i.e. db.article.aggregate(
{ $group : { _id : "$author", docsPerAuthor : { $sum : 1 }, viewsPerAuthor : { $sum : "$pageViews" } }});
6.$sort 吃内存
 i.e. db.users.aggregate(
{ $sort : { age : -1, posts: 1 } });
Expression介绍:
提供了强大的运算符和各种函数

相关资源链接:
 API
 聚合框架目录
阅读(1546) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~