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 }
});
结果:
|