学习MongoDB 十一: MongoDB聚合(Aggregation Pipeline基础篇上)(三)

一、Aggregate简介 


                db.collection.aggregate()是基于数据处理的聚合管道,每个文档通过一个由多个阶段(stage)组成的管道,可以对每个阶段的管道进行分组、过滤等功能,然后经过一系列的处理,输出相应的结果。

               

       图来自https://docs.mongodb.com/manual/aggregation/ 官方网

        我们通过这张图,可以清晰的了解Aggregate处理的过程

  

         1db.collection.aggregate()可以多个管道,能方便的进行数据的处理。

         2db.collection.aggregate()使用了MongoDB内置的原生操作,聚合效率非常高,支持类似于SQL Group By操作的功能,而不再需要用户编写自定义的JavaScript例程。

         3 每个阶段管道限制为100MB内存。如果一个节点管道超过这个极限,MongoDB将产生一个错误。为了能够在处理大型数据集,可以设置allowDiskUsetrue来在聚合管道节点把数据写入临时文件。这样就可以解决100MB内存的限制。

         4、db.collection.aggregate()可以作用在分片集合,但结果不能输在分片集合,MapReduce可以 作用在分片集合,结果也可以输在分片集合。

    5、db.collection.aggregate()方法可以返回一个指针(cursor),数据放在内存中,直接操作。跟Mongo shell 一样指针操作。

         6db.collection.aggregate()输出的结果只能保存在一个文档中,BSON Document大小限制为16M。可以通过返回指针解决,版本2.6中后面:DB.collect.aggregate()方法返回一个指针,可以返回任何结果集的大小。

         

 二、aggregate语法:

 

      db.collection.aggregate(pipeline, options)

 

     【pipeline  $group参数】

 

         pipeline 类型是Array  语法: db.collection.aggregate( [ { <stage> }, ... ] ) 

       $group : 将集合中的文档分组,可用于统计结果,$group首先将数据根据key进行分组。

      $group语法: { $group: { _id: <expression>, <field1>: { <accumulator1> : <expression1> }, ... } }

          _id 是要进行分组的key

       $group:可以分组的数据执行如下的表达式计算:

           $sum:计算总和。

           $avg:计算平均值。

           $min:根据分组,获取集合中所有文档对应值得最小值。

           $max:根据分组,获取集合中所有文档对应值得最大值。

           $push:将指定的表达式的值添加到一个数组中。

           $addToSet:将表达式的值添加到一个集合中(无重复值)。

           $first:返回每组第一个文档,如果有排序,按照排序,如果没有按照默认的存储的顺序的第一个文档。

           $last:返回每组最后一个文档,如果有排序,按照排序,如果没有按照默认的存储的顺序的最后个文档。

 

     我们可以通过Aggregation pipeline一些使用sql用法一样,我们能很清晰的怎么去使用

 

           pipeline                                           sql

           $avg                                                 avg

           $min                                                min

           $max                                               max 

           $group                                             group by

           $sort                                               order by

           $limit                                               limit                   

           $sum                                              sum()

           $sum                                              count()


   三、pipeline $group 简单的例子


      【数据 】 

[sql]  view plain  copy
  1. db.items.insert( [  
  2.   {  
  3.    "quantity" : 2,  
  4.    "price" : 5.0,  
  5.    "pnumber" : "p003",  
  6.   },{  
  7.    "quantity" : 2,  
  8.    "price" : 8.0,  
  9.    "pnumber" : "p002"  
  10.   },{  
  11.    "quantity" : 1,  
  12.    "price" : 4.0,  
  13.    "pnumber" : "p002"  
  14.   },{  
  15.    "quantity" : 2,  
  16.    "price" : 4.0,  
  17.    "pnumber" : "p001"  
  18.   },{  
  19.    "quantity" : 4,  
  20.    "price" : 10.0,  
  21.    "pnumber" : "p003"  
  22.   },{  
  23.    "quantity" : 10,  
  24.    "price" : 20.0,  
  25.    "pnumber" : "p001"  
  26.   },{  
  27.    "quantity" : 10,  
  28.    "price" : 20.0,  
  29.    "pnumber" : "p003"  
  30.   },{  
  31.    "quantity" : 5,  
  32.    "price" : 10.0,  
  33.    "pnumber" : "p002"  
  34.   }  
  35. ])       


    【$group】

 

      1、将集合中的文档分组,可用于统计结果,$group首先将数据根据key进行分组。

             _id 是要进行分组的key,如果_idnull  相当于select  count(*) from table


  【 $sum】  

     1、 我们统计items有几条,相当于SQL:  select count(1) as count from items

[sql]  view plain  copy
  1. > db.items.count()  
  2. 8  
  3. > db.items.aggregate([{$group:{_id:null,count:{$sum:1}}}])  
  4. "_id" : null"count" : 8 }  

    2、我们统计一下数量,相当于SQL: select sum(quantity) as total  from  items

[sql]  view plain  copy
  1. > db.items.aggregate([{$group:{_id:null,total:{$sum:"$quantity"}}}])  
  2. "_id" : null"total" : 36 }  


  3、我们通过产品类型来进行分组,然后在统计卖出的数量是多少,相当于SQL:select sum(quantity) as total from  items  group by pnumber

[sql]  view plain  copy
  1.   > db.items.aggregate([{$group:{_id:"$pnumber",total:{$sum:"$quantity"}}}])  
  2. "_id" : "p001""total" : 12 }  
  3. "_id" : "p002""total" : 8 }  
  4. "_id" : "p003""total" : 16 }  


【$min 、 $max 】 

   1、我们通过相同的产品类型来进行分组,然后查询相同产品类型卖出最多的订单详情 ,相当于SQL: select max(quantity) as quantity from  items  group by pnumber

[sql]  view plain  copy
  1. > db.items.aggregate([{$group:{_id:"$pnumber",max:{$max:"$quantity"}}}])  
  2. "_id" : "p001""max" : 10 }  
  3. "_id" : "p002""max" : 5 }  
  4. "_id" : "p003""max" : 10 }  

    2、我们通过相同的产品类型来进行分组,然后查询相同产品类型卖出最多的订单详情 ,相当于SQL:select min(quantity) as quantity from  items  group by pnumber

[sql]  view plain  copy
  1. > db.items.aggregate([{$group:{_id:"$pnumber",min:{$min:"$quantity"}}}])  
  2. "_id" : "p001""min" : 2 }  
  3. "_id" : "p002""min" : 1 }  
  4. "_id" : "p003""min" : 2 }  

  3、我们通过相同的产品类型来进行分组,统计各个产品数量,然后获取最大的数量,相当于SQL: select max(t.total) from (select sum(quantity) as total from  items  group by pnumber) t

[sql]  view plain  copy
  1. > db.items.aggregate([{$group:{_id:"$pnumber",total:{$sum:"$quantity"}}}])  
  2. "_id" : "p001""total" : 12 }  
  3. "_id" : "p002""total" : 8 }  
  4. "_id" : "p003""total" : 16 }  
  5. > db.items.aggregate([{$group:{_id:"$pnumber",total:{$sum:"$quantity"}}},{$group:{_id:null,max:{$max:"$total"}}}])  
  6. "_id" : null"max" : 16 }  

【$avg】

    先根据$group,在计算平均值,只会针对数字的进行计算,会对字符串忽略

 

    1、我们通过相同的产品类型来进行分组,然后查询每个订单详情相同产品类型卖出的平均价格,相当于SQL:select avg(price) as price from  items  group by pnumber

[sql]  view plain  copy
  1.    
  2. > db.items.aggregate([{$group:{_id:"$pnumber",price:{$avg:"$price"}}}])  
  3. "_id" : "p001""price" : 12 }  
  4. "_id" : "p002""price" : 7.333333333333333 }  
  5. "_id" : "p003""price" : 11.666666666666666 }  

【$push】

      将指定的表达式的值添加到一个数组中,这个值不要超过16M,不然会出现错误

    1、我们通过相同的产品类型来进行分组,然后查询每个相同产品卖出的数量放在数组里面

[sql]  view plain  copy
  1. > db.items.aggregate([{$group:{_id:"$pnumber",quantitys:{$push:"$quantity"}}}])  
  2. "_id" : "p001""quantitys" : [ 2, 10 ] }  
  3. "_id" : "p002""quantitys" : [ 2, 1, 5 ] }  
  4. "_id" : "p003""quantitys" : [ 2, 4, 10 ] }  
[sql]  view plain  copy
  1. > db.items.aggregate([{$group:{_id:"$pnumber",quantitys:{$push:{quantity:"$quantity",price:"$price"}}}}])  
  2. "_id" : "p001""quantitys" : [ { "quantity" : 2, "price" : 4 }, { "quantity": 10, "price" : 20 } ] }  
  3. "_id" : "p002""quantitys" : [ { "quantity" : 2, "price" : 8 }, { "quantity": 1, "price" : 4 }, { "quantity" : 5, "price" : 10 } ] }  
  4. "_id" : "p003""quantitys" : [ { "quantity" : 2, "price" : 5 }, { "quantity": 4, "price" : 10 }, { "quantity" : 10, "price" : 20 } ] }  


【 $addToSet】

       将表达式的值添加到一个数组中(无重复值),这个值不要超过16M,不然会出现错误

[sql]  view plain  copy
  1. > db.items.aggregate([{$group:{_id:"$pnumber",quantitys:{$addToSet:"$quantity"}}}])  
  2. "_id" : "p001""quantitys" : [ 10, 2 ] }  
  3. "_id" : "p002""quantitys" : [ 5, 1, 2 ] }  
  4. "_id" : "p003""quantitys" : [ 10, 4, 2 ] }  


【 $first、 $last】

    $first:返回每组第一个文档,如果有排序,按照排序,如果没有按照默认的存储的顺序的第一个文档。

    $last:返回每组最后一个文档,如果有排序,按照排序,如果没有按照默认的存储的顺序的最后个文档。

[sql]  view plain  copy
  1. > db.items.aggregate([{$group:{_id:"$pnumber",quantityFrist:{$first:"$quantity"}}}])  
  2. "_id" : "p001""quantityFrist" : 2 }  
  3. "_id" : "p002""quantityFrist" : 2 }  
  4. "_id" : "p003""quantityFrist" : 2 }  


      我们这篇主要介绍了aggregate  pipeline的$group 基础操作,后续介绍了 pipeline其他参数和options使用
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值