3.用提取的数据当key名
从上面的例子我们可以知道文档的标签都是我们一开始就定义的,那能不能用从数据库中提取的数据来当标签呢?
例子如下:
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={};
values.forEach(function(val) {
reduced[val.country+val.province+val.city+"_temprature"]=(val.temprature);
reduced[val.country+val.province+val.city+"_weather"]=(val.weather);
});
return reduced;
},
finalize : function Finalize(key, reduced) {
return reduced;
},
out : { inline : 1 }
});
结果:
|