您好,登錄后才能下訂單哦!
日期分組 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; } } });
先插入測(cè)試數(shù)據(jù):
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聯(lián)合$where查詢
db.test.group({key:{age:true},initial:{num:0},$reduce:function(doc,prev){
prev.num++
},
condition:{$where:function(){
return this.age>2;
}
}
});
3.使用函數(shù)返回值分組
//注意,$keyf指定的函數(shù)一定要返回一個(gè)對(duì)象
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.使用終結(jié)器
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; }
}
});
有關(guān)MapReduce
//首先插入測(cè)試數(shù)據(jù)
for(var i=1;i<21;i++)
{
db.test.insert({_id:i,name:'mm'+i});
}
//進(jìn)行mapreduce
db.runCommand(
{
mapreduce:'test',
map:function(){emit(this.name.substr(0,3),this);},
reduce:function(key,vals){return vals[0];}, //注意:vals是一個(gè)Object對(duì)象而不是數(shù)組
out:'wq'
});
注意:
1.mapreduce是根據(jù)map函數(shù)里調(diào)用的emit函數(shù)的第一個(gè)參數(shù)來(lái)進(jìn)行分組的
2.僅當(dāng)根據(jù)分組鍵分組后一個(gè)鍵匹配多個(gè)文檔,才會(huì)將key和文檔集合交由reduce函數(shù)處理。例如:
db.runCommand(
{
mapreduce:'test',
map:function(){emit(this.name.substr(0,3),this);},
reduce:function(key,vals){return 'wq';},
out:'wq'
});
執(zhí)行mapreduce命令后,再查看wq表數(shù)據(jù):
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" } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。