MongoDB2.2.0新特性之Aggregation Framework
来源:互联网 发布:手机网络个人理财产品 编辑:程序博客网 时间:2024/05/17 09:45
2.2.0新加的一个很好用的框架
两个概念:pipeline类似于linux shell中的管道操作,用于对文档进行过滤和修改;
expression用于对pipeline指令进行逻辑运算
如何使用?db.people.aggregate( [<pipeline>] )
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介绍:
提供了强大的运算符和各种函数
相关资源链接:
http://docs.mongodb.org/manual/reference/aggregation/ API
http://docs.mongodb.org/manual/aggregation/#aggregation-framework 聚合框架目录
- MongoDB2.2.0新特性之Aggregation Framework
- MongoDB2.6 新特性
- Mongodb2.6升级修改以及新特性
- .NET Framework 2.0 Beta的新特性
- Entity Framework Core 2.0 新特性
- Entity Framework Core 2.0 新特性
- .NET Framework 3.0新特性
- .NET Framework 3.5新特性
- .net framework 3.5 新特性
- NET Framework 3.5新特性
- .NET FrameWork 4.0 新特性
- .net framework 4.5 新特性
- .NET Framework 4.5新特性
- .NET Framework 4.5新特性
- .NET Framework 4.5新特性
- Mongodb Aggregation Framework
- 介绍.NET Framework 3.0新特性
- Zend Framework V1.5 中的新特性
- 我指间的刺青是对你的誓言
- 二叉树遍历 非递归 C++实现
- 9.25号实验课要求已经放在课件下载中。请提前作一下。
- oracle数据库出现maximum open cursors exceed问题
- The 37th ACM/ICPC Asia Regional HangZhou Site Online Contest - F
- MongoDB2.2.0新特性之Aggregation Framework
- oracle 10g rac 监听名称:CRS-0210: Could not find resource
- 游戏引擎与游戏引擎开发入门
- 尕福(散文)
- dump block的内容解析(varchar2 number date)实验
- HDU 4419 Colourful Rectangle 线段树求矩阵的并升级版again
- BitmapData.draw
- fork ,world hellow怎么办
- va_list/va_start/va_arg/va_end深入分析