1.mongoTemplate使用MapReduce统计
//统计问题 map,我放在resources下
String map = "classpath:MapReduce.js";
//问题统计 reduce
String reduce = "classpath:reduce.js";
MapReduceResults<HashMap> out = null;
try {
out = mongoTemplate.mapReduce("表名", map, reduce, HashMap.class);
} catch (Exception e) {
e.printStackTrace();
}
Iterator iterator = out.iterator();
while (iterator.hasNext()) {
HashMap it = (HashMap) iterator.next();
String key = (String) it.get("_id");//_id存的是emit中的key,例如tag或者txt
Map value = (Map) it.get("value"); //返回的数据结构,例如{'123_txt':1,'123_txt':5},值是统计的数值
}
2.MapReduce.js文件代码,mongo获取每一行都会执行这段代码,this代表当前行的document,详情可参考:http://www.yayihouse.com/yayishuwu/chapter/2843