mapReduce函数原型
db.collection.mapReduce(
<map>,
<reduce>,
{
out: <collection>,
query: <document>,
sort: <document>,
limit: <number>,
finalize: <function>,
scope: <document>,
jsMode: <boolean>,
verbose: <boolean>
}
)
mapReduce函数关键是map和reduce函数的编写!!
map
函数一般比较简单,仅仅包含emit(key,value)
函数
key、value都要是json格式!!
如果key或value仅包含一个字段,可以直接返回。
{
_id:ObjectId("XXXXXXXXXXXXXXXXXXXXXX"),
name:"Jack",
age:28,
sex:"male",
address:"XXXX",
income:10000
}
仅仅返回单个字段:
emit(sex,age)
返回多个字段:
emit({"sex":this.sex,"address":this.address},{"age":this.age,"income":this.income,"count":1})
value中字段可以自己任意定义,count:1
用于统计数目
function reduce(key,values)
reduce函数默认有两个传入参数,
- key即map阶段的key
- values是map阶段emit对具有相同key的记录进行合并后的结果,是数组
reduce函数只负责处理values内容,不对key进行处理,返回的处理结果也必须符合json格式,不含key内容
function reduce(key, values) {
var reduced = {
age : 0,
income : 0,
count : 0
};
values.forEach(function (val) {
reduced.age += val.age;
reduced.income += val.income;
reduced.count += val.count;
});
return reduced;
}
finalize函数在Reduce处理结束,返回结果之前再做一次处理,optional字段
function Finalize(key, reduced) {
reduced.avgAge = reduced.age / reduced.count;
reduced.avgIncome = reduced.income / reduced.count;
return reduced;
}
其他字段不详细解释,可自行查看官网相关文档
本文实现的mapReduce函数是根据性别和地址,统计平均年龄和收入