mongodb - Can I get group and count in aggregation framework? -


i have score collection like:

{grade:1 group:1, alumn:1, score:100}, {grade:1 group:1, alumn:1, score:100}, {grade:1 group:1, alumn:2, score:80}, {grade:1 group:1, alumn:2, score:80}, {grade:1 group:2, alumn:1, score:70}, {grade:1 group:2, alumn:1, score:70}, {grade:1 group:2, alumn:2, score:80}, {grade:1 group:2, alumn:2, score:80}, {grade:1 group:3, alumn:1, score:90}, {grade:1 group:3, alumn:1, score:90}, {grade:1 group:3, alumn:2, score:90}, {grade:1 group:3, alumn:2, score:90}, 

how can best alumn of each grade/group?

grade:1

[{group:1, alumn:1, score:200}], [{group:2, alumn:2, score:160}], [{group:3, alumn:1, score:180},  {group:3, alumn:2, score:180}] 

this query return output needed :

    var scores = db.scores.aggregate({     $group: {         _id: {             grade: "$grade",             group: "$group",             alumn: "$alumn"         },         score: {             $sum: "$score"         }     }     }, {         $project: {             _id: 0,             group: "$_id.group",             alumn: "$_id.alumn",             score: "$score"     }     }, {         $sort: {             group: 1,             score: -1     } }); 

we can't directly first document alone, have written js part:

from first query, get:

{         "result" : [                {                         "score" : 200,                         "group" : 1,                         "alumn" : 1                 },                 {                         "score" : 160,                         "group" : 1,                         "alumn" : 2                 },                 {                         "score" : 160,                         "group" : 2,                         "alumn" : 2                 },                 {                         "score" : 140,                         "group" : 2,                         "alumn" : 1                 },                 {                         "score" : 180,                         "group" : 3,                         "alumn" : 2                 },                 {                         "score" : 180,                         "group" : 3,                         "alumn" : 1                 }         ],         "ok" : 1 } 

now comes filtering part,

function duplicate(array,index, value) {   var matched = false;   for(var i=0; i<array.length; i++) {    if(i<index) {       if(value==array[i].group) {         return false;       }     } else {      return true;     }   } } 

now call

scores.result.filter(function(e,index) {return duplicate(scores.result,index,e.group)}); 

this give you

       [         {                 "score" : 200,                 "group" : 1,                 "alumn" : 1         },         {                 "score" : 160,                 "group" : 2,                 "alumn" : 2         },         {                 "score" : 180,                 "group" : 3,                 "alumn" : 2         }     ] 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -