Board logo

标题: mongodb mapreduce分析(8) [打印本页]

作者: look_w    时间: 2019-4-16 19:04     标题: mongodb mapreduce分析(8)

2.分组后把同一组的数据放在同一条文档中(foreach--数组.push)

如果我们经过省分组后,但是又想把每一个省其中的城市的数据都取出来,而不是只取一个城市。那可以用如下方法:

db.runCommand({ mapreduce: "c",
map : function Map() {


emit(
  this.province,     // how to group
  {country:this.country,province:this.province,city:this.city,temprature:this.temprature,weather:this.weather} // associated data point (document)
);


},
reduce : function Reduce(key, values) {

     var reduced={country:{},province:{},city:[],temprature:[],weather:[]};

values.forEach(function(val) {
reduced.country=val.country;
reduced.province=val.province;
reduced.city.push(val.city);
reduced.temprature.push(val.temprature);
  reduced.weather.push(val.weather);
});

return reduced;

},
finalize : function Finalize(key, reduced) {


return reduced;
},
out : { inline : 1 }
});



结果:






欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0