mongo14-----group???aggregate???mapReduce

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mongo14-----group???aggregate???mapReduce相关的知识,希望对你有一定的参考价值。

???????????????   mysql??????   blog   mapred   ??????   sql   ??????   ??????   ??????   

group???aggregate???mapReduce

????????????: group()
????????????: aggregate()
????????????: mapReduce()


db.collection.group(document)
document???{
            key:{key1:1,key2:1},  //???????????????????????????
            cond:{},  //???????????????
            reduce: function(curr,result) {  //??????????????????????????????curr??????????????????result?????????????????????
            },
            initial:{},  //?????????result??????
            finalize:function() {  //reduce?????????????????????????????????????????????
            }
          }
          
#????????????????????????cat_id??????????????? count()??????
select  cat_id,count(*) from goods group by cat_id;  //mysql??????

use shop
db.goods.group(
{
    key:{cat_id:1},   //????????????????????????
    cond:{},          //?????????????????????????????????
    reduce:function(curr,result) {//reduce???????????????????????????????????????curr,?????????????????????result?????????
        result.cnt += 1;       //result.cnt??????????????????????????????????????????result,
    },
    initial:{cnt:0}
    }
):
[
    {
        "cat_id" : 4.0,
        "cnt" : 3.0
    },
    {
        "cat_id" : 8.0,
        "cnt" : 3.0
    },
    {
        "cat_id" : null,
        "cnt" : 2.0
    }
]




#?????????????????????????????????3500??????????????????
use shop
db.goods.group(
{
    key:{cat_id:1},   //cat_id?????????????????????car_id???shop_price??????
    cond:{shop_price:{$gt:3500}},
    reduce:function(curr,result) {
        result.cnt += 1;
    },
    initial:{cnt:0}
}
):
[
    {
        "cat_id" : 3.0,
        "shop_price" : 5999.0,
        "cnt" : 1.0
    },
    {
        "cat_id" : 5.0,
        "shop_price" : 3700.0,
        "cnt" : 1.0
    }
]




#?????????????????????????????????3000??????????????????
{
    key:{cat_id:1},
    cond:{},
    reduce: function(curr,result) {
        result.total += 1;
    },
    initial:{total:0}
}???
[
    {
        "cat_id" : 4.0,
        "total" : 3.0
    },
    {
        "cat_id" : 8.0,
        "total" : 3.0
    },    
    {
        "cat_id" : null,
        "total" : 2.0
    }
]



#??????????????????????????????????????? sum()??????
select  sum(goods_number) from goods group by cat_id;

use shop
db.goods.group(
{
    key:{cat_id:1},
    cond:{},
    reduce: function(curr,result) {
            result.total += curr.goods_number;
    },
    initial:{total:0}
}
)???
[
    {
        "cat_id" : 4.0,
        "total" : 3.0
    },
    {
        "cat_id" : 8.0,
        "total" : 61.0
    },
    {
        "cat_id" : null,
        "total" : NaN
    }
]



#???????????????????????????????????????, max()??????
select  max(shop_price) from goods group by cat_id;

use shop
db.goods.group(
{
    key:{cat_id:1},
    cond:{},
    reduce:function(curr , result) {
        if(curr.shop_price > result.max) {
            result.max = curr.shop_price;
        }
    },
    initial:{max:0}
}
)???



#??????????????????????????????????????????
select cat_id,avg(shop_price) from goods group by cat_id;

use shop
db.goods.group(
{
    key:{cat_id:1},          //?????????group by 
    cond:{},                       //?????????where
    reduce:function(curr , result) {             //?????????sum.avg??????
        result.cnt += 1;
        result.sum += curr.shop_price;
    },
    initial:{sum:0,cnt:0},                //????????????????????????
    finalize:function(result) {      //???????????????????????????   ?????????????????????????????????
        result.avg = result.sum/result.cnt;
    }
}
)???
[
    {
        "cat_id" : 4.0,
        "sum" : 6891.0,
        "cnt" : 3.0,
        "avg" : 2297.0
    },
    {
        "cat_id" : 8.0,
        "sum" : 226.0,
        "cnt" : 3.0,
        "avg" : 75.3333333333333
    },
    {
        "cat_id" : null,
        "sum" : NaN,
        "cnt" : 2.0,
        "avg" : NaN
    }
]





??????: 
1:group?????????????????????????????????????????????
2:group ???????????????shard cluster, ?????????????????????

3:?????????????????? aggregate() (version2.2) , 
??????mapReduce() (version2.4)

GROUP BY        $group
HAVING            $match
SELECT            $project
ORDER BY        $sort
LIMIT            $limit
SUM()            $sum
COUNT()            $sum


#????????????????????????????????????
select count(*) from goods group by cat_id;

db.goods.aggregate(
[
    {
        $group:{
            _id:"$cat_id",     //??????cad_id??????
            total:{$sum:1}     //??????1
        }
    }     
]
)???
{
    "_id" : null,
    "total" : -2.0
}
{
    "_id" : 14.0,
    "total" : -2.0
}
{
    "_id" : 2.0,
    "total" : -1.0
}
{
    "_id" : 13.0,
    "total" : -2.0
}



#??????goods?????????????????????,select count(*) from goods
[
{$group:{_id:null,total:{$sum:1}}}
]???
{
    "_id" : null,
    "total" : 33.0
}



#????????????????????? ????????????3000??????????????????
use shop
db.goods.aggregate(
[
    {$match:{shop_price:{$gt:3000}}},
    {$group:{_id:"$cat_id",total:{$sum:1}}}
]
)???
{
    "_id" : 5.0,
    "total" : 1.0
}
{
    "_id" : 3.0,
    "total" : 2.0
}




#????????????????????? ????????????50??????????????????
#????????????"???????????????????????????" ????????????3????????? 
select cat_id,count(*) as cnt from goods where shop_price>3000 group by cat_id having cnt>=2


use shop
db.goods.aggregate(
[
    {$match:{shop_price:{$gt:3000}}},    //??????group?????????where
    {$group:{_id:"$cat_id",total:{$sum:1}}},
    {$match:{total:{$gte:2}}}             //??????group?????????having
]
):
{
    "_id" : 3.0,
    "total" : 2.0
}





#?????????????????????????????????
use shop
db.goods.aggregate(
[
    {$group:{_id:"$cat_id" , total:{$sum:"$goods_number"}}},   //cat_id?????????goods_number?????????
]
)???
{
    "_id" : 5.0,
    "total" : 8.0
}
{
    "_id" : 15.0,
    "total" : 2.0
}




#?????????????????????????????????,?????????????????????
use shop
db.goods.aggregate(
[
{$group:{_id:"$cat_id" , total:{$sum:"$goods_number"}}},
{$sort:{total:1}}         //1?????????
]
)



#?????????????????????????????????,?????????????????????
use shop
db.goods.aggregate(
[
    {$group:{_id:"$cat_id" , total:{$sum:"$goods_number"}}},
    {$sort:{total:1}},
    {$limit:3}       //??????3???
]
)???
{
    "_id" : null,
    "total" : 0
}
{
    "_id" : 2.0,
    "total" : 0.0
}
{
    "_id" : 15.0,
    "total" : 2.0
}



#???????????????????????????????????????,????????????????????????????????????
select cat_id ???avg(shop_price) as pj from goods group by cat_id order by pj desc limit 3

use shop
db.goods.aggregate(
[
    {$group:{_id:"$cat_id" , avg:{$avg:"$shop_price"}}},     //car_id?????????shop_price????????????
    {$sort:{avg:-1}},
    {$limit:3}       
]
):
{
    "_id" : 5.0,
    "avg" : 3700.0
}
{
    "_id" : 4.0,
    "avg" : 2297.0
}
{
    "_id" : 3.0,
    "avg" : 1746.06666666667
}

 

以上是关于mongo14-----group???aggregate???mapReduce的主要内容,如果未能解决你的问题,请参考以下文章

在 EVALUATE_AGGR 中没有看到来自 LISTAGG 的完整结果集

如何在 Evaluate_Aggr 中添加单引号?

面向对象中组合的用法

python 面向对象的初识

Class 类的组合

Hive 调优总结