关于MongoDB的group分组 先插入测试数据: for(var i=1; i20; i++){ var num=i%6; db.test.insert({_id:i,name:user_+i,age:num}); } 1.普通分组查询 db.test.group({key:{age:true},initial:{num:0},$reduce:function(doc,prev){ prev.num++ }}); db.runCom
关于MongoDB的group分组
先插入测试数据:
for(var i=1; i<20; i++){
var num=i%6;
db.test.insert({_id:i,name:"user_"+i,age:num});
}
1.普通分组查询
db.test.group({key:{age:true},initial:{num:0},$reduce:function(doc,prev){
prev.num++
}});
db.runCommand({group:
{
ns:"test",
key:{age:true},
initial:{num:0},
$reduce:function(doc,prev){
prev.num++}
}
});
2.筛选后再分组
db.test.group({key:{age:true},initial:{num:0},$reduce:function(doc,prev){
prev.num++
},
condition:{age:{$gt:2}}
});
db.runCommand({group:
{
ns:"test",
key:{age:true},
initial:{num:0},
$reduce:function(doc,prev){
prev.num++},
condition:{age:{$gt:2}}
}
});
普通的$where查询:
db.test.find({$where:function(){
return this.age>2;
}
});
group联合$where查询
db.test.group({key:{age:true},initial:{num:0},$reduce:function(doc,prev){
prev.num++
},
condition:{$where:function(){
return this.age>2;
}
}
});
3.使用函数返回值分组
//注意,$keyf指定的函数一定要返回一个对象
db.test.group({$keyf:function(doc){return {age:doc.age};},initial:{num:0},$reduce:function(doc,prev){
prev.num++
}
});
db.runCommand({group:
{
ns:"test",
$keyf:function(doc){return {age:doc.age};},
initial:{num:0},
$reduce:function(doc,prev){
prev.num++}
}
});
4.使用终结器
db.test.group({$keyf:function(doc){return {age:doc.age};},initial:{num:0},$reduce:function(doc,prev){
prev.num++
},
finalize: function(doc){ doc.count=doc.num;delete doc.num; }
});
db.runCommand({group:
{
ns:"test",
$keyf:function(doc){return {age:doc.age};},
initial:{num:0},
$reduce:function(doc,prev){
prev.num++},
finalize: function(doc){ doc.count=doc.num;delete doc.num; }
}
});
有关MapReduce
//首先插入测试数据
for(var i=1;i<21;i++)
{
db.test.insert({_id:i,name:&#39;mm&#39;+i});
}
//进行mapreduce
db.runCommand(
{
mapreduce:&#39;test&#39;,
map:function(){emit(this.name.substr(0,3),this);},
reduce:function(key,vals){return vals[0];}, //注意:vals是一个Object对象而不是数组
out:&#39;wq&#39;
});
注意:
1.mapreduce是根据map函数里调用的emit函数的第一个参数来进行分组的
2.仅当根据分组键分组后一个键匹配多个文档,才会将key和文档集合交由reduce函数处理。例如:
db.runCommand(
{
mapreduce:&#39;test&#39;,
map:function(){emit(this.name.substr(0,3),this);},
reduce:function(key,vals){return &#39;wq&#39;;},
out:&#39;wq&#39;
});
执行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" } }