在学习使用Mongodb中,对map和reduce的定义,以及对 mapReduce() 方法的调用:
>map #在直接输入map时会出现一个'map is not defined'的异常错误!
>reduce #同上
根据mongodb的handbook来参考,需要自定义这两个函数:
>mapFun1 function() {
eimt(this.name,{count:1};
}
>reduceFun1 function(key,value) {
var result={count:0};
for (var i=0;i<value.length;i++) {
result.count += value[i].count;
}
return result;
}
>db.person.mapReduce(mapFun1,reduceFun1,{"out":"collection"});
{
"result" : "collection",
"timeMillis" : 122,
"counts" : {
"input" : 5,
"emit" : 5,
"reduce" : 0,
"output" : 5
},
"ok" : 1,
}
>db.collection.find();
{ "_id" : "A1", "value" : { "count" : 1 } }
{ "_id" : "A2", "value" : { "count" : 1 } }
{ "_id" : "A3", "value" : { "count" : 1 } }
{ "_id" : "A4", "value" : { "count" : 1 } }
{ "_id" : "A5", "value" : { "count" : 1 } }