Red de conocimiento informático - Aprendizaje de programación - El controlador MongoDB implementa funciones agregadas de suma y promedio

El controlador MongoDB implementa funciones agregadas de suma y promedio

Las funciones de agregación como MongoDB sum y avg AVG son relativamente débiles y se pueden implementar con MapReduce. Déjame darte un ejemplo.

※//※※※※※※※※※※Carga de datos※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※Carga de datos※

d b .proc .remove();

for(var I = 10;ilt1020;i){ db.proc.insert({ clase: "01", val:i, nombre: "nombre" I } )};

for(var I = 10;ilt1030;i){db.proc.insert({class: "02", val:i, nombre: "nombre" I})} ;

for(var I = 10; i lt1040; i ){db.proc.insert({clase: "03 ", val: i, nombre: " nombre " I })}

p>

for(var I = 10; i lt1050; i ){db.proc.insert({clase: "04 ", val: i, nombre: " nombre " I })}

for(var I = 10;ilt1060;i){db.proc.insert({class: "05", val:i, nombre: "nombre" I})};

for ( var I = 10;ilt1070;i){db.proc.insert({class: "06", val:i, nombre: "nombre" I })};

for(var I = 10 ;ilt1080;i){db.proc.insert({clase: "07", val:i, nombre: "nombre" I })};

for(var I = 10; ilt1090;i ){db.proc.insert({clase: "08", val:i, nombre: "nombre" I })};

for(var I = 10; ilt1100; i ){db. proc.insert({clase: "09", val: i, nombre: " nombre " I })};

for (var I = 10; i lt1110; i ){ db insert . ({ clase: " 10 ", val: i, nombre: " nombre " I })}

for (var I = 10; i lt1120; i ) { db insert({ class . : " 11 ", val: i, nombre: " nombre " I })};

※ mapReduce, mapReduce, mapReduce, mapReduce

m = function(){emit(this .class, {count: 1, max: this.val, sum: this.val})}

r = función (clave, valor){

var ct = 0, sm = 0, MX = 0;

for (var I = 0; i ltvalores. longitud; i ) {

ct = valores [i]. contar;

sm = valor[i]. max

mx = Math.max(mx, valores[i]).

max);

}

return {count: ct, max: mx, sum: sm}; /※※※※※※※※※※※※※※※※※※※※※※※※※※※※※Procesamiento de datos※

RES = db ejecutar comando({ MapReduce: " proc ", map: m, reduce: r, out: "proc_res"})

RES = db run command({ MapReduce: " proc ", map: m, reduce: r, out: "proc_res3", consulta: {"class": {$gt: "03"}})

RES = db run command({ MapReduce: " proc ", map: m, reduce: r, salida: "proc_res5", consulta: {"class": {$gt: "05"}})

RES = db run command({ MapReduce: " proc ", map: m, reduce: r, salida: "proc_res6", consulta: {"class": {$gt: "06"}})

RES = db run command({ MapReduce: " proc ", map: m, reducir: r, salida: "proc_res9 ", consulta: {"class": {$gt: "09"}})

RES = db ejecutar comando({ MapReduce: " proc ", mapa: m, reducir: r, salir: "proc_res10 ", consulta: { " class ": { $ gt: " 10 " } })

//※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ※.

db.proc_res.find()

{ "_id": 1, "valor": { "clase": 1, "recuento": 10, " suma": 145, "max": 19 } }

{ "_id": 2, "valor": { "clase": 2, "recuento": 20, "suma": 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, "valor": { "clase": 5, "recuento": 50, "suma": 1725, "max": 59 } }

{ "_id": 6, "valor": { "clase": 6, "recuento": 6

0, "suma": NaN, "max": NaN } }

{ "_id": 7, "valor": { "clase": 7, "recuento": 70, "suma": 3115, "max": 79 } }

{ "_id": 8, "value": { "class": 8, "count": 80, "sum": NaN, "max": NaN } }

{ "_id": 9, "valor": { "clase": 9, "count": 90, "suma": NaN, "max": NaN } }

{ "_id": 10, "valor": { "clase": 10, "recuento": 100, "suma": NaN, "max": NaN } }

{ " _id": 11, "valor": { "clase": 11, "recuento": 110, "suma": NaN, "max": NaN } }

db.proc_res3.find()

{ "_id": 4, "valor": { "clase": 4, "recuento": 40, "suma": 1180, "max": 49 } }

{ "_id": 5, "valor": { "clase": 5, "recuento": 50, "suma": 1725, "max": 59 } }

{ "_id": 6 , "valor": { "clase": 6, "recuento": 60, "suma": NaN, "max": NaN } }

{ "_id": 7, "valor": { "clase": 7, "recuento": 70, "suma": NaN, "max": NaN } }

{ "_id": 8, "valor": { "clase": 8, "recuento": 80, "suma": 3960, "máximo": 89 } }

{ "_id": 9, "valor": { "clase": 9, "recuento": 90, "suma": 4905, "max": 99 } }

{ "_id": 10, "valor": { "clase": 10, "recuento": 100, "suma": NaN, "max": NaN } }

{ "_id": 11, "value": { "class": 11, "count": 110, "sum": NaN, "max": NaN } }

db.proc_res5.find()

{ "_id": 6, "valor": { "clase": 6, "recuento": 60, "suma": 2370, "max": 69 } }

{ "_id": 7, "value": { "class": 7, "count": 70, "sum": NaN, "max": NaN } }

{ "_id": 8, "valor": { "clase": 8, "recuento": 80, "suma": NaN, "max": NaN } }

{ "_id": 9, "valor": { "clase": 9, "recuento": 90, "suma": 4905, "max": 99 } }

{ "_id": 10, "valor": { "clase": 10, "recuento": 100, "suma": 5950, "máximo": 109 } }

{ "_id": 11, "valor": { "clase": 11, "recuento": 110, "suma": NaN, "máx.": NaN } }

db.proc_res6.find()

{ "_id ": 7, "valor": { "clase": 7, "recuento": 70, "suma": 3115, "máximo": 79 } }

{ "_id": 8, "valor " ": { "clase": 8, "recuento": 80, "suma": NaN, "max": NaN } }

{ "_id": 9, "valor": { "clase " : 9, "recuento" : 90, "suma" : NaN, "max" : NaN } }

{ "_id" : 10, "valor" : { "clase" : 10, "recuento " : 100, "suma" : NaN, "max" : NaN } }

{ "_id" : 11, "valor" : { "clase" : 11, "recuento" : 110, "suma " : NaN, "max" : NaN } }

db.proc_res9.find()

{ "_id": 10, "valor" : { "clase": 10, "recuento": 100, "suma": 5950, "máximo": 109 } }

{ "_id": 11, "valor": { "clase": 11, "recuento": 110, " suma": NaN, "max": NaN } }

db.proc_res10.find()

{ "_id": 11, "valor": { "clase": 11, "recuento": 110, "suma": NaN, "máximo": NaN } }