MongoDB驱动 实现 sum和 avg聚合函数
1个回答
2013-09-25
展开全部
MongoDB sum,avg等聚合函数很弱,可以用MapReduce来实现,我给你个例子
// ※※※※※※※※※※※※※※※※※※※ 数据加载 ※※※※※※※※※※※※※※※※※※※
db.proc.remove();
for(var i = 10; i < 1020; i++){db.proc.insert({class:"01", val:i, name: "name" + i})};
for(var i = 10; i < 1030; i++){db.proc.insert({class:"02", val:i, name: "name" + i})};
for(var i = 10; i < 1040; i++){db.proc.insert({class:"03", val:i, name: "name" + i})};
for(var i = 10; i < 1050; i++){db.proc.insert({class:"04", val:i, name: "name" + i})};
for(var i = 10; i < 1060; i++){db.proc.insert({class:"05", val:i, name: "name" + i})};
for(var i = 10; i < 1070; i++){db.proc.insert({class:"06", val:i, name: "name" + i})};
for(var i = 10; i < 1080; i++){db.proc.insert({class:"07", val:i, name: "name" + i})};
for(var i = 10; i < 1090; i++){db.proc.insert({class:"08", val:i, name: "name" + i})};
for(var i = 10; i < 1100; i++){db.proc.insert({class:"09", val:i, name: "name" + i})};
for(var i = 10; i < 1110; i++){db.proc.insert({class:"10", val:i, name: "name" + i})};
for(var i = 10; i < 1120; i++){db.proc.insert({class:"11", val:i, name: "name" + i})};
// ※※※※※※※※※※※※※※※※※※※ mapReduce ※※※※※※※※※※※※※※※※※※※
m = function(){emit(this.class, {count:1, max:this.val, sum:this.val})}
r = function(key, values){
var ct = 0, sm = 0, mx = 0;
for(var i = 0; i < values.length; i++){
ct += values[i].count;
sm += values[i].max;
mx = Math.max(mx, values[i].max);
}
return {count:ct, max: mx, sum:sm};
}
// ※※※※※※※※※※※※※※※※※※※ 数据处理 ※※※※※※※※※※※※※※※※※※※
res = db.runCommand({mapreduce:"proc", map:m, reduce:r, out:"proc_res"})
res = db.runCommand({mapreduce:"proc", map:m, reduce:r, out:"proc_res3", query:{"class":{$gt:"03"}}})
res = db.runCommand({mapreduce:"proc", map:m, reduce:r, out:"proc_res5", query:{"class":{$gt:"05"}}})
res = db.runCommand({mapreduce:"proc", map:m, reduce:r, out:"proc_res6", query:{"class":{$gt:"06"}}})
res = db.runCommand({mapreduce:"proc", map:m, reduce:r, out:"proc_res9", query:{"class":{$gt:"09"}}})
res = db.runCommand({mapreduce:"proc", map:m, reduce:r, out:"proc_res10",query:{"class":{$gt:"10"}}})
// ※※※※※※※※※※※※※※※※※※※ 结果查看 ※※※※※※※※※※※※※※※※※※※
db.proc_res.find()
{ "_id" : 1, "value" : { "class" : 1, "count" : 10, "sum" : 145, "max" : 19 } }
{ "_id" : 2, "value" : { "class" : 2, "count" : 20, "sum" : 390, "max" : 29 } }
{ "_id" : 3, "value" : { "class" : 3, "count" : 30, "sum" : 735, "max" : 39 } }
{ "_id" : 4, "value" : { "class" : 4, "count" : 40, "sum" : 1180, "max" : 49 } }
{ "_id" : 5, "value" : { "class" : 5, "count" : 50, "sum" : 1725, "max" : 59 } }
{ "_id" : 6, "value" : { "class" : 6, "count" : 60, "sum" : NaN, "max" : NaN } }
{ "_id" : 7, "value" : { "class" : 7, "count" : 70, "sum" : 3115, "max" : 79 } }
{ "_id" : 8, "value" : { "class" : 8, "count" : 80, "sum" : NaN, "max" : NaN } }
{ "_id" : 9, "value" : { "class" : 9, "count" : 90, "sum" : NaN, "max" : NaN } }
{ "_id" : 10, "value" : { "class" : 10, "count" : 100, "sum" : NaN, "max" : NaN } }
{ "_id" : 11, "value" : { "class" : 11, "count" : 110, "sum" : NaN, "max" : NaN } }
db.proc_res3.find()
{ "_id" : 4, "value" : { "class" : 4, "count" : 40, "sum" : 1180, "max" : 49 } }
{ "_id" : 5, "value" : { "class" : 5, "count" : 50, "sum" : 1725, "max" : 59 } }
{ "_id" : 6, "value" : { "class" : 6, "count" : 60, "sum" : NaN, "max" : NaN } }
{ "_id" : 7, "value" : { "class" : 7, "count" : 70, "sum" : NaN, "max" : NaN } }
{ "_id" : 8, "value" : { "class" : 8, "count" : 80, "sum" : 3960, "max" : 89 } }
{ "_id" : 9, "value" : { "class" : 9, "count" : 90, "sum" : 4905, "max" : 99 } }
{ "_id" : 10, "value" : { "class" : 10, "count" : 100, "sum" : NaN, "max" : NaN } }
{ "_id" : 11, "value" : { "class" : 11, "count" : 110, "sum" : NaN, "max" : NaN } }
db.proc_res5.find()
{ "_id" : 6, "value" : { "class" : 6, "count" : 60, "sum" : 2370, "max" : 69 } }
{ "_id" : 7, "value" : { "class" : 7, "count" : 70, "sum" : NaN, "max" : NaN } }
{ "_id" : 8, "value" : { "class" : 8, "count" : 80, "sum" : NaN, "max" : NaN } }
{ "_id" : 9, "value" : { "class" : 9, "count" : 90, "sum" : 4905, "max" : 99 } }
{ "_id" : 10, "value" : { "class" : 10, "count" : 100, "sum" : 5950, "max" : 109 } }
{ "_id" : 11, "value" : { "class" : 11, "count" : 110, "sum" : NaN, "max" : NaN } }
db.proc_res6.find()
{ "_id" : 7, "value" : { "class" : 7, "count" : 70, "sum" : 3115, "max" : 79 } }
{ "_id" : 8, "value" : { "class" : 8, "count" : 80, "sum" : NaN, "max" : NaN } }
{ "_id" : 9, "value" : { "class" : 9, "count" : 90, "sum" : NaN, "max" : NaN } }
{ "_id" : 10, "value" : { "class" : 10, "count" : 100, "sum" : NaN, "max" : NaN } }
{ "_id" : 11, "value" : { "class" : 11, "count" : 110, "sum" : NaN, "max" : NaN } }
db.proc_res9.find()
{ "_id" : 10, "value" : { "class" : 10, "count" : 100, "sum" : 5950, "max" : 109 } }
{ "_id" : 11, "value" : { "class" : 11, "count" : 110, "sum" : NaN, "max" : NaN } }
db.proc_res10.find()
{ "_id" : 11, "value" : { "class" : 11, "count" : 110, "sum" : NaN, "max" : NaN } }
// ※※※※※※※※※※※※※※※※※※※ 数据加载 ※※※※※※※※※※※※※※※※※※※
db.proc.remove();
for(var i = 10; i < 1020; i++){db.proc.insert({class:"01", val:i, name: "name" + i})};
for(var i = 10; i < 1030; i++){db.proc.insert({class:"02", val:i, name: "name" + i})};
for(var i = 10; i < 1040; i++){db.proc.insert({class:"03", val:i, name: "name" + i})};
for(var i = 10; i < 1050; i++){db.proc.insert({class:"04", val:i, name: "name" + i})};
for(var i = 10; i < 1060; i++){db.proc.insert({class:"05", val:i, name: "name" + i})};
for(var i = 10; i < 1070; i++){db.proc.insert({class:"06", val:i, name: "name" + i})};
for(var i = 10; i < 1080; i++){db.proc.insert({class:"07", val:i, name: "name" + i})};
for(var i = 10; i < 1090; i++){db.proc.insert({class:"08", val:i, name: "name" + i})};
for(var i = 10; i < 1100; i++){db.proc.insert({class:"09", val:i, name: "name" + i})};
for(var i = 10; i < 1110; i++){db.proc.insert({class:"10", val:i, name: "name" + i})};
for(var i = 10; i < 1120; i++){db.proc.insert({class:"11", val:i, name: "name" + i})};
// ※※※※※※※※※※※※※※※※※※※ mapReduce ※※※※※※※※※※※※※※※※※※※
m = function(){emit(this.class, {count:1, max:this.val, sum:this.val})}
r = function(key, values){
var ct = 0, sm = 0, mx = 0;
for(var i = 0; i < values.length; i++){
ct += values[i].count;
sm += values[i].max;
mx = Math.max(mx, values[i].max);
}
return {count:ct, max: mx, sum:sm};
}
// ※※※※※※※※※※※※※※※※※※※ 数据处理 ※※※※※※※※※※※※※※※※※※※
res = db.runCommand({mapreduce:"proc", map:m, reduce:r, out:"proc_res"})
res = db.runCommand({mapreduce:"proc", map:m, reduce:r, out:"proc_res3", query:{"class":{$gt:"03"}}})
res = db.runCommand({mapreduce:"proc", map:m, reduce:r, out:"proc_res5", query:{"class":{$gt:"05"}}})
res = db.runCommand({mapreduce:"proc", map:m, reduce:r, out:"proc_res6", query:{"class":{$gt:"06"}}})
res = db.runCommand({mapreduce:"proc", map:m, reduce:r, out:"proc_res9", query:{"class":{$gt:"09"}}})
res = db.runCommand({mapreduce:"proc", map:m, reduce:r, out:"proc_res10",query:{"class":{$gt:"10"}}})
// ※※※※※※※※※※※※※※※※※※※ 结果查看 ※※※※※※※※※※※※※※※※※※※
db.proc_res.find()
{ "_id" : 1, "value" : { "class" : 1, "count" : 10, "sum" : 145, "max" : 19 } }
{ "_id" : 2, "value" : { "class" : 2, "count" : 20, "sum" : 390, "max" : 29 } }
{ "_id" : 3, "value" : { "class" : 3, "count" : 30, "sum" : 735, "max" : 39 } }
{ "_id" : 4, "value" : { "class" : 4, "count" : 40, "sum" : 1180, "max" : 49 } }
{ "_id" : 5, "value" : { "class" : 5, "count" : 50, "sum" : 1725, "max" : 59 } }
{ "_id" : 6, "value" : { "class" : 6, "count" : 60, "sum" : NaN, "max" : NaN } }
{ "_id" : 7, "value" : { "class" : 7, "count" : 70, "sum" : 3115, "max" : 79 } }
{ "_id" : 8, "value" : { "class" : 8, "count" : 80, "sum" : NaN, "max" : NaN } }
{ "_id" : 9, "value" : { "class" : 9, "count" : 90, "sum" : NaN, "max" : NaN } }
{ "_id" : 10, "value" : { "class" : 10, "count" : 100, "sum" : NaN, "max" : NaN } }
{ "_id" : 11, "value" : { "class" : 11, "count" : 110, "sum" : NaN, "max" : NaN } }
db.proc_res3.find()
{ "_id" : 4, "value" : { "class" : 4, "count" : 40, "sum" : 1180, "max" : 49 } }
{ "_id" : 5, "value" : { "class" : 5, "count" : 50, "sum" : 1725, "max" : 59 } }
{ "_id" : 6, "value" : { "class" : 6, "count" : 60, "sum" : NaN, "max" : NaN } }
{ "_id" : 7, "value" : { "class" : 7, "count" : 70, "sum" : NaN, "max" : NaN } }
{ "_id" : 8, "value" : { "class" : 8, "count" : 80, "sum" : 3960, "max" : 89 } }
{ "_id" : 9, "value" : { "class" : 9, "count" : 90, "sum" : 4905, "max" : 99 } }
{ "_id" : 10, "value" : { "class" : 10, "count" : 100, "sum" : NaN, "max" : NaN } }
{ "_id" : 11, "value" : { "class" : 11, "count" : 110, "sum" : NaN, "max" : NaN } }
db.proc_res5.find()
{ "_id" : 6, "value" : { "class" : 6, "count" : 60, "sum" : 2370, "max" : 69 } }
{ "_id" : 7, "value" : { "class" : 7, "count" : 70, "sum" : NaN, "max" : NaN } }
{ "_id" : 8, "value" : { "class" : 8, "count" : 80, "sum" : NaN, "max" : NaN } }
{ "_id" : 9, "value" : { "class" : 9, "count" : 90, "sum" : 4905, "max" : 99 } }
{ "_id" : 10, "value" : { "class" : 10, "count" : 100, "sum" : 5950, "max" : 109 } }
{ "_id" : 11, "value" : { "class" : 11, "count" : 110, "sum" : NaN, "max" : NaN } }
db.proc_res6.find()
{ "_id" : 7, "value" : { "class" : 7, "count" : 70, "sum" : 3115, "max" : 79 } }
{ "_id" : 8, "value" : { "class" : 8, "count" : 80, "sum" : NaN, "max" : NaN } }
{ "_id" : 9, "value" : { "class" : 9, "count" : 90, "sum" : NaN, "max" : NaN } }
{ "_id" : 10, "value" : { "class" : 10, "count" : 100, "sum" : NaN, "max" : NaN } }
{ "_id" : 11, "value" : { "class" : 11, "count" : 110, "sum" : NaN, "max" : NaN } }
db.proc_res9.find()
{ "_id" : 10, "value" : { "class" : 10, "count" : 100, "sum" : 5950, "max" : 109 } }
{ "_id" : 11, "value" : { "class" : 11, "count" : 110, "sum" : NaN, "max" : NaN } }
db.proc_res10.find()
{ "_id" : 11, "value" : { "class" : 11, "count" : 110, "sum" : NaN, "max" : NaN } }
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询