Here is the script example you can edit it and use in as per your requirement,
MongoDB Script:
db.collectionname.group({
"key": {
"UserName": true
},
"initial": {
"sumscore": 0,
"sumforaverageaveragescore": 0,
"countforaverageaveragescore": 0,
"countstar": 0
},
"reduce": function(obj, prev) {
prev.sumscore = prev.sumscore + obj.score - 0;
prev.sumforaverageaveragescore += obj.score;
prev.countforaverageaveragescore++;
prev.minimumvaluescore = isNaN(prev.minimumvaluescore) ? obj.score : Math.min(prev.minimumvaluescore, obj.score);
prev.maximumvaluescore = isNaN(prev.maximumvaluescore) ? obj.score : Math.max(prev.maximumvaluescore, obj.score);
if (true != null) if (true instanceof Array) prev.countstar += true.length;
else prev.countstar++;
},
"finalize": function(prev) {
prev.averagescore = prev.sumforaverageaveragescore / prev.countforaverageaveragescore;
delete prev.sumforaverageaveragescore;
delete prev.countforaverageaveragescore;
},
"cond": {
"score": {
"$gt": 0
},
"UserName": {
"$in": ["shivam", "anonymous"]
}
}
});
its SQL script is:
SELECT UserName, SUM(score), AVG(score), MIN(score), MAX(score), COUNT(*)
FROM collectionname
WHERE score > 0 AND UserName IN('shivam','anonymous')
GROUP BY UserName;
Let me know if it fulfills your requirement.Thanks.