mongo14

1group,aggregate,mapReduce 2 3分组统计: group() 4简单聚合: aggregate() 5强大统计: mapReduce() 6 7 8db.collection.group(document) 9document{ 10 key:{key1:1,key2:1}, //根据那几个字段分组 11 cond:{}, //筛选的条件 12 reduce: function(curr,result) { //分组之后的聚合运算,curr是一行数据,result是计算后的结果 13 }, 14 initial:{}, //初始化result里面 15 finalize:function() { //reduce一组都执行完毕后最后执行的函数 16 } 17 } 18 19#计算每个栏目下(cat_id)的商品数 count()操作 20select cat_id,count(*) from goods group by cat_id; //mysql操作 21 22use shop 23db.goods.group( 24{ 25 key:{cat_id:1}, //根据哪个字段分组 26 cond:{}, //所有行取出来,不加条件 27 reduce:function(curr,result) {//reduce的执行过程:每一行就是一个curr,每一组共用一个result变量, 28 result.cnt += 1; //result.cnt是每组有多少行,每个组有一个result, 29 }, 30 initial:{cnt:0} 31 } 32): 33[ 34 { 35 "cat_id" : 4.0, 36 "cnt" : 3.0 37 }, 38 { 39 "cat_id" : 8.0, 40 "cnt" : 3.0 41 }, 42 { 43 "cat_id" : null, 44 "cnt" : 2.0 45 } 46] 47 48 49 50 51#查询每个栏目下价格高于3500元的商品数量 52use shop 53db.goods.group( 54{ 55 key:{cat_id:1}, //cat_id分组,并且查出car_id和shop_price字段 56 cond:{shop_price:{$gt:3500}}, 57 reduce:function(curr,result) { 58 result.cnt += 1; 59 }, 60 initial:{cnt:0} 61} 62): 63[ 64 { 65 "cat_id" : 3.0, 66 "shop_price" : 5999.0, 67 "cnt" : 1.0 68 }, 69 { 70 "cat_id" : 5.0, 71 "shop_price" : 3700.0, 72 "cnt" : 1.0 73 } 74] 75 76 77 78 79#查询每个栏目下价格大于3000元的商品个数 80{ 81 key:{cat_id:1}, 82 cond:{}, 83 reduce: function(curr,result) { 84 result.total += 1; 85 }, 86 initial:{total:0} 87}88[ 89 { 90 "cat_id" : 4.0, 91 "total" : 3.0 92 }, 93 { 94 "cat_id" : 8.0, 95 "total" : 3.0 96 }, 97 { 98 "cat_id" : null, 99 "total" : 2.0 100 } 101] 102 103 104 105#计算每个栏目下的商品库存量 sum()操作 106select sum(goods_number) from goods group by cat_id; 107 108use shop 109db.goods.group( 110{ 111 key:{cat_id:1}, 112 cond:{}, 113 reduce: function(curr,result) { 114 result.total += curr.goods_number; 115 }, 116 initial:{total:0} 117} 118)119[ 120 { 121 "cat_id" : 4.0, 122 "total" : 3.0 123 }, 124 { 125 "cat_id" : 8.0, 126 "total" : 61.0 127 }, 128 { 129 "cat_id" : null, 130 "total" : NaN 131 } 132] 133 134 135 136#查询每个栏目最贵的商品价格, max()操作 137select max(shop_price) from goods group by cat_id; 138 139use shop 140db.goods.group( 141{ 142 key:{cat_id:1}, 143 cond:{}, 144 reduce:function(curr , result) { 145 if(curr.shop_price > result.max) { 146 result.max = curr.shop_price; 147 } 148 }, 149 initial:{max:0} 150} 151)152 153 154 155#查询每个栏目下商品的平均价格 156select cat_id,avg(shop_price) from goods group by cat_id; 157 158use shop 159db.goods.group( 160{ 161 key:{cat_id:1}, //相当于group by 162 cond:{}, //相当于where 163 reduce:function(curr , result) { //相当于sum.avg函数 164 result.cnt += 1; 165 result.sum += curr.shop_price; 166 }, 167 initial:{sum:0,cnt:0}, //进这个组执行一下 168 finalize:function(result) { //出这个组执行一下, 组操作完毕后的回调函数 169 result.avg = result.sum/result.cnt; 170 } 171} 172)173[ 174 { 175 "cat_id" : 4.0, 176 "sum" : 6891.0, 177 "cnt" : 3.0, 178 "avg" : 2297.0 179 }, 180 { 181 "cat_id" : 8.0, 182 "sum" : 226.0, 183 "cnt" : 3.0, 184 "avg" : 75.3333333333333 185 }, 186 { 187 "cat_id" : null, 188 "sum" : NaN, 189 "cnt" : 2.0, 190 "avg" : NaN 191 } 192] 193 194 195 196 197 198注意: 1991:group需要我们手写聚合函数的业务逻辑 2002:group 不支持集群shard cluster, 无法分布式运算 201 2023:分布式可以用 aggregate() (version2.2) , 203或者mapReduce() (version2.4) 204 205GROUP BY $group 206HAVING $match 207SELECT $project 208ORDER BY $sort 209LIMIT $limit 210SUM() $sum 211COUNT() $sum 212 213 214#查询每个栏目下的商品数量 215select count(*) from goods group by cat_id; 216 217db.goods.aggregate( 218[ 219 { 220 $group:{ 221 _id:"$cat_id", //根据cad_id分组 222 total:{$sum:1} //乘以1 223 } 224 } 225] 226)227{ 228 "_id" : null, 229 "total" : -2.0 230} 231{ 232 "_id" : 14.0, 233 "total" : -2.0 234} 235{ 236 "_id" : 2.0, 237 "total" : -1.0 238} 239{ 240 "_id" : 13.0, 241 "total" : -2.0 242} 243 244 245 246#查询goods下有多少条商品,select count(*) from goods 247[ 248{$group:{_id:null,total:{$sum:1}}} 249]250{ 251 "_id" : null, 252 "total" : 33.0 253} 254 255 256 257#查询每个栏目下 价格大于3000元的商品个数 258use shop 259db.goods.aggregate( 260[ 261 {$match:{shop_price:{$gt:3000}}}, 262 {$group:{_id:"$cat_id",total:{$sum:1}}} 263] 264)265{ 266 "_id" : 5.0, 267 "total" : 1.0 268} 269{ 270 "_id" : 3.0, 271 "total" : 2.0 272} 273 274 275 276 277#查询每个栏目下 价格大于50元的商品个数 278#并筛选出"满足条件的商品个数" 大于等于3的栏目 279select cat_id,count(*) as cnt from goods where shop_price>3000 group by cat_id having cnt>=2 280 281 282use shop 283db.goods.aggregate( 284[ 285 {$match:{shop_price:{$gt:3000}}}, //放在group之前是where 286 {$group:{_id:"$cat_id",total:{$sum:1}}}, 287 {$match:{total:{$gte:2}}} //放在group之后是having 288] 289): 290{ 291 "_id" : 3.0, 292 "total" : 2.0 293} 294 295 296 297 298 299#查询每个栏目下的库存量 300use shop 301db.goods.aggregate( 302[ 303 {$group:{_id:"$cat_id" , total:{$sum:"$goods_number"}}}, //cat_id分组,goods_number求和, 304] 305)306{ 307 "_id" : 5.0, 308 "total" : 8.0 309} 310{ 311 "_id" : 15.0, 312 "total" : 2.0 313} 314 315 316 317 318#查询每个栏目下的库存量,并按库存量排序 319use shop 320db.goods.aggregate( 321[ 322{$group:{_id:"$cat_id" , total:{$sum:"$goods_number"}}}, 323{$sort:{total:1}} //1是升序 324] 325) 326 327 328 329#查询每个栏目下的库存量,并按库存量排序 330use shop 331db.goods.aggregate( 332[ 333 {$group:{_id:"$cat_id" , total:{$sum:"$goods_number"}}}, 334 {$sort:{total:1}}, 335 {$limit:3} //取前3个 336] 337)338{ 339 "_id" : null, 340 "total" : 0 341} 342{ 343 "_id" : 2.0, 344 "total" : 0.0 345} 346{ 347 "_id" : 15.0, 348 "total" : 2.0 349} 350 351 352 353#查询每个栏目的商品平均价格,并按平均价格由高到低排序 354select cat_id ,avg(shop_price) as pj from goods group by cat_id order by pj desc limit 3 355 356use shop 357db.goods.aggregate( 358[ 359 {$group:{_id:"$cat_id" , avg:{$avg:"$shop_price"}}}, //car_id排序,shop_price求平均, 360 {$sort:{avg:-1}}, 361 {$limit:3} 362] 363): 364{ 365 "_id" : 5.0, 366 "avg" : 3700.0 367} 368{ 369 "_id" : 4.0, 370 "avg" : 2297.0 371} 372{ 373 "_id" : 3.0, 374 "avg" : 1746.06666666667 375} 376 377mapReduce 随着"大数据"概念而流行,mapReduce的真正强项在于分布式。 378其实mapReduce的概念非常简单,比aggregate要简单,从功能上说,相当于RDBMS(传统数据库)的 group 操作。 379 380当数据非常大时,像google,N多数据中心,数据都不在地球的一端,用group力所不及.group既然不支持分布式, 由于单台服务器的运算能力必然是有限的. 381 382而mapRecuce支持分布式(不是算法好),而是支持大量的服务器同时工作,用蛮力来统计.mapRecuce就是group和aggregate,只不过支持分布式。 383 384mapRecuce的工作过程:1.map-->映射,2.reduce->归约 385 386map: 1.先在全世界机器找(分布式集群上找),把属于同一个组的数据,映射到一个数组上.cat_id [23,2,6,7]2.reduce: 把数组(同一组)的数据,进行运算. 387 388 389 390 391 392#用mapReduce计算每个栏目的库存总量 393 394//map函数(进行映射工作,映射成一个二维数组) 395var map = function() { 396 emit(this.cat_id,this.goods_number); //根据cat_id分组, 397} 398 399/* 400{ 401 cat_id1:[goods_number1,goods_number2,goods_number3.....], 402 cat_id2:[goods_number1,goods_number2,goods_number3.....] 403 cat_id3:[goods_number1,goods_number2,goods_number3.....] 404} 405*/ 406 407var reduce = function(cat_id,numbers) { //对数组做处理,求goods_number的和, 408 return Array.sum(numbers); //mongo对js的数组增加的求和方法 409} 410 411/* 412{ 413 _id:cat_id1, value:goods_number1+goods_number2+goods_number3....., 414 _id:cat_id1, value:goods_number1+goods_number2+goods_number3....., 415 _id:cat_id1, value:goods_number1+goods_number2+goods_number3....., 416} 417*/ 418 419db.goods.mapReduce(map,reduce,{out:'res'}); //out计算的结果放在res集合里面去, 420//多了一个res表 421show tables 422db.res.find(): 423{ 424 "_id" : null, 425 "value" : NaN 426} 427{ 428 "_id" : 2.0, 429 "value" : 0.0 430} 431{ 432 "_id" : 3.0, 433 "value" : 203.0 434} 435{ 436 "_id" : 4.0, 437 "value" : 3.0 438} 439{ 440 "_id" : 15.0, 441 "value" : 2.0 442} 443 444//查看array的所有方法: 445for (var k in Array){ 446 print(k) 447}448contains 449unique 450shuffle 451tojson 452fetchRefs 453sum 454avg 455stdDev 456 457 458 459 460#用mapReduce计算每个栏目下商品的平均价格 461var map = function() { 462 emit(this.cat_id,this.shop_price); 463} 464var reduce = function(cat_id,values) { 465 return Array.avg(values); 466} 467db.goods.mapReduce(map,reduce,{out:'res'}); 468469{ 470 "_id" : null, 471 "value" : NaN 472} 473{ 474 "_id" : 2.0, 475 "value" : 823.33 476} 477{ 478 "_id" : 3.0, 479 "value" : 1746.06666666667 480} 481 482var map = function() { 483 if(this.jing < 0 || this.wei < 0){ 484 return; 485 } 486 var j = Math.floor(this.jing/5)*5; 487 var w = Math.floor(this.wei/5)*5; 488 var block = j+":"+w; 489 emit(block,1); 490} 491var reduce = function(block,values) { 492 return Array.sum(values); 493} 494db.goods.mapReduce(map,reduce,{out:'res'});
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )