错误
ReferenceError: map is not defined
操作
> map
2017-02-23T14:52:43.197+0800 ReferenceError: map is not defined
> reduce
2017-02-23T14:52:52.121+0800 ReferenceError: reduce is not defined
> db.user.mapReduce(map,reduce,{"out":"collection"})
2017-02-23T14:54:13.693+0800 ReferenceError: map is not defined
解释:
map跟reduce未定义
解决
1、先查看集合
> db.user.find()
{ "_id" : ObjectId("58ae37681263f741cb833fc2"), "name" : "keilson", "age" : 30,
"gender" : "M" }
{ "_id" : ObjectId("58ae39a11263f741cb833fc3"), "name" : "Tom", "age" : 20, "gen
der" : "M" }
{ "_id" : ObjectId("58ae81c38f72bc301b434c6a"), "name" : "lili", "age" : 25, "ge
nder" : "F" }
{ "_id" : ObjectId("58ae82118f72bc301b434c6b"), "name" : "lili", "age" : 25, "ge
nder" : "F" }
2、定义map跟reduce
var map= function(){emit(this.name,this.age);}
var reduce = function(key,values){return Array.sum(values)}
db.user.mapReduce(map,reduce,{"out":"collection"})
"result" : "collection",
"timeMillis" : 53,
"counts" : {
"input" : 4,
"emit" : 4,
"reduce" : 1,
"output" : 3
},
"ok" : 1
}