日期分组 db.msds_acce***ecord.group({ keyf : function(doc){ var date = new Date(doc.addtime); var dateKey = ""+date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate(); return {'day':dateKey}; //33}, initial : {"count":0}, reduce : function Reduce(doc, out) { if(doc.url){ out.count +=1; } } });
目前成都创新互联已为千余家的企业提供了网站建设、域名、虚拟空间、网站运营、企业网站设计、荔湾网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
先插入测试数据:
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:'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" } }