[
{
"age" : ,
"num" :
},
{
"age" : ,
"num" :
},
{
"age" : ,
"num" :
},
{
"age" : ,
"num" :
},
{
"age" : ,
"num" :
},
{
"age" : ,
"num" :
}
]
有关MapReduce
for(var i=1;i<21;i++)
{
db.test.insert({_id:i,name:'mm'+i});
}
//进行mapreduce
db.runCommand(
{
mapreduce:'test',
map:function(){emit(this.name.substr(0,3),this);},
reduce:function(key,vals){return vals[0];}, //注意:vals是一个Object对象而不是数组
out:'wq'
});
注意:
1.mapreduce是根据map函数里调用的emit函数的第一个参数来进行分组的
2.仅当根据分组键分组后一个键匹配多个文档,才会将key和文档集合交由reduce函数处理。例如:
db.runCommand(
{
mapreduce:'test',
map:function(){emit(this.name.substr(0,3),this);},
reduce:function(key,vals){return 'wq';},
out:'wq'
});
执行mapreduce命令后,再查看wq表数据:
db.wq.find()
{ "_id" : "mm1", "value" : "wq" }
{ "_id" : "mm2", "value" : "wq" }
{ "_id" : "mm3", "value" : { "_id" : 3, "name" : "mm3" } }
{ "_id" : "mm4", "value" : { "_id" : 4, "name" : "mm4" } }
{ "_id" : "mm5", "value" : { "_id" : 5, "name" : "mm5" } }
{ "_id" : "mm6", "value" : { "_id" : 6, "name" : "mm6" } }
{ "_id" : "mm7", "value" : { "_id" : 7, "name" : "mm7" } }
{ "_id" : "mm8", "value" : { "_id" : 8, "name" : "mm8" } }
{ "_id" : "mm9", "value" : { "_id" : 9, "name" : "mm9" } }