1、数据准备
[{"goods_id":1,"cat_id":4,"goods_name":"KD876","goods_number":1,"click_count":7,"shop_price":1388.00,"add_time":1240902890},{"goods_id":4,"cat_id":8,"goods_name":"u8bfau57fau4e9aN85u539fu88c5u5145u7535u5668","goods_number":17,"click_count":0,"shop_price":58.00,"add_time":1241422402},{"goods_id":3,"cat_id":8,"goods_name":"u8bfau57fau4e9au539fu88c55800u8033u673a","goods_number":24,"click_count":3,"shop_price":68.00,"add_time":1241422082},{"goods_id":5,"cat_id":11,"goods_name":"u7d22u7231u539fu88c5M2u5361u8bfbu5361u5668","goods_number":8,"click_count":3,"shop_price":20.00,"add_time":1241422518},{"goods_id":6,"cat_id":11,"goods_name":"u80dcu521bKINGMAXu5185u5b58u5361","goods_number":15,"click_count":0,"shop_price":42.00,"add_time":1241422573},{"goods_id":7,"cat_id":8,"goods_name":"u8bfau57fau4e9aN85u539fu88c5u7acbu4f53u58f0u8033u673aHS-82","goods_number":20,"click_count":0,"shop_price":100.00,"add_time":1241422785},{"goods_id":8,"cat_id":3,"goods_name":"u98deu5229u6d669@9v","goods_number":1,"click_count":9,"shop_price":399.00,"add_time":1241425512},{"goods_id":9,"cat_id":3,"goods_name":"u8bfau57fau4e9aE66","goods_number":4,"click_count":20,"shop_price":2298.00,"add_time":1241511871},{"goods_id":10,"cat_id":3,"goods_name":"u7d22u7231C702c","goods_number":7,"click_count":11,"shop_price":1328.00,"add_time":1241965622},{"goods_id":11,"cat_id":3,"goods_name":"u7d22u7231C702c","goods_number":1,"click_count":0,"shop_price":1300.00,"add_time":1241966951},{"goods_id":12,"cat_id":3,"goods_name":"u6469u6258u7f57u62c9A810","goods_number":8,"click_count":13,"shop_price":983.00,"add_time":1245297652}] [{"goods_id":13,"cat_id":3,"goods_name":"u8bfau57fau4e9a5320 XpressMusic","goods_number":8,"click_count":13,"shop_price":1311.00,"add_time":1241967762},{"goods_id":14,"cat_id":4,"goods_name":"u8bfau57fau4e9a5800XM","goods_number":1,"click_count":6,"shop_price":2625.00,"add_time":1241968492},{"goods_id":15,"cat_id":3,"goods_name":"u6469u6258u7f57u62c9A810","goods_number":3,"click_count":8,"shop_price":788.00,"add_time":1241968703},{"goods_id":16,"cat_id":2,"goods_name":"u6052u57fau4f1fu4e1aG101","goods_number":0,"click_count":3,"shop_price":823.33,"add_time":1241968949},{"goods_id":17,"cat_id":3,"goods_name":"u590fu65b0N7","goods_number":1,"click_count":2,"shop_price":2300.00,"add_time":1241969394},{"goods_id":18,"cat_id":4,"goods_name":"u590fu65b0T5","goods_number":1,"click_count":0,"shop_price":2878.00,"add_time":1241969533},{"goods_id":19,"cat_id":3,"goods_name":"u4e09u661fSGH-F258","goods_number":12,"click_count":7,"shop_price":858.00,"add_time":1241970139},{"goods_id":20,"cat_id":3,"goods_name":"u4e09u661fBC01","goods_number":12,"click_count":14,"shop_price":280.00,"add_time":1241970417},{"goods_id":21,"cat_id":3,"goods_name":"u91d1u7acb A30","goods_number":40,"click_count":4,"shop_price":2000.00,"add_time":1241970634},{"goods_id":22,"cat_id":3,"goods_name":"u591au666eu8fbeTouch HD","goods_number":1,"click_count":15,"shop_price":5999.00,"add_time":1241971076}] [{"goods_id":23,"cat_id":5,"goods_name":"u8bfau57fau4e9aN96","goods_number":8,"click_count":17,"shop_price":3700.00,"add_time":1241971488},{"goods_id":24,"cat_id":3,"goods_name":"P806","goods_number":100,"click_count":35,"shop_price":2000.00,"add_time":1241971981},{"goods_id":25,"cat_id":13,"goods_name":"u5c0fu7075u901a/u56fau8bdd50u5143u5145u503cu5361","goods_number":2,"click_count":0,"shop_price":48.00,"add_time":1241972709},{"goods_id":26,"cat_id":13,"goods_name":"u5c0fu7075u901a/u56fau8bdd20u5143u5145u503cu5361","goods_number":2,"click_count":0,"shop_price":19.00,"add_time":1241972789},{"goods_id":27,"cat_id":15,"goods_name":"u8054u901a100u5143u5145u503cu5361","goods_number":2,"click_count":0,"shop_price":95.00,"add_time":1241972894},{"goods_id":28,"cat_id":15,"goods_name":"u8054u901a50u5143u5145u503cu5361","goods_number":0,"click_count":0,"shop_price":45.00,"add_time":1241972976},{"goods_id":29,"cat_id":14,"goods_name":"u79fbu52a8100u5143u5145u503cu5361","goods_number":0,"click_count":0,"shop_price":90.00,"add_time":1241973022},{"goods_id":30,"cat_id":14,"goods_name":"u79fbu52a820u5143u5145u503cu5361","goods_number":9,"click_count":1,"shop_price":18.00,"add_time":1241973114},{"goods_id":31,"cat_id":3,"goods_name":"u6469u6258u7f57u62c9E8 ","goods_number":1,"click_count":5,"shop_price":1337.00,"add_time":1242110412},{"goods_id":32,"cat_id":3,"goods_name":"u8bfau57fau4e9aN85","goods_number":4,"click_count":9,"shop_price":3010.00,"add_time":1242110760}]
2、group参数解析
官方给出参数结构:
{
group:
{
ns: <namespace>,
key: <key>,
$reduce: <reduce function>,
$keyf: <key function>,
cond: <query>,
finalize: <finalize function>
}
}
重要参数解析:
key:作为分组的key
reduce:这些函数可以返回一个sum或count。该函数接受两个参数(curr,result):curr(当前文档)和result(分组聚集的结果文档)。
initial:初始化聚合结果文档变量,为空时自动为每列提供初始变量。
keyf:可选。替代的key 字段。指定一个函数创建一个“key object”作为分组的key。使用keyf而是通过group by领域而不是现有的文档域键组。
cond:过滤条件
finalize:在db.collection.group()返回最终结果之前,此功能可以修改的结果文档或替换的结果文档作为一个整体。
3、执行顺序解析
1、key-->2、initial-->3、cond-->4、reduce 将本组信息存储,然后其他组循环2,3,4执行
4、执行案例
#key:表示分组的字段 #cond:分组后查询的条件 #reduce:聚合操作,其中curr表示当前文档的指针,result表示分组聚集的文档 #initial:参数初始化操作 #finalize:最终执行函数,类似java中的finally代码块 #查询每个栏目下的商品数量 { key:{cat_id:1}, cond:{}, reduce:function(curr,result) { result.cnt += 1; }, initial:{cnt:0} } #查询每个栏目下价格高于50元的商品数量 { key:{cat_id:1}, cond:{shop_price:{$gt:50}}, reduce:function(curr,res){ res.cnt +=1; }, initial:{cnt:0} } #每个栏目下的商品库存量 sum()操作 { key:{cat_id:1}, cond:{}, reduce:function(curr,res){ res.num +=curr.goods_number; }, initial:{num:0} } #查询每个栏目最贵的商品价格, max()操作 { key:{cat_id:1}, cond:{}, reduce:function(curr,res){ if(res.price<curr.shop_price){ res.price = curr.shop_price; } }, initial:{price:0} } #查询每个栏目下商品的平均价格 { key:{cat_id:1}, cond:{}, reduce:function(curr,res){ res.cnt +=1; res.sum += curr.shop_price; }, initial:{sum:0,cnt:0}, finalize:function(res){ res.avg = res.sum/res.cnt; } }
欢迎留言,一起学习、进步。