How to mimic SQL group, count(*), join, union and derived (in reactive way)

yeh but it’s pretty much same as it’s stores the same data? find().fetch() just turns mongodata to identical array

Tested on different set of data, the script works fine.

1 Like

> Here is the complete one!

Took only 11h to complete this without any skills :joy:

memo  = GameMemo.find({event_type: "Goals"}).fetch();

goalsArray = _.countBy(memo, "player_name_event");    
assistsArray = _.countBy(memo, "player_name_event_first");
assistsArray = _.countBy(memo, "player_name_event_second");

var assists = _.map(assistsArray, function(value, key) {
return {player_name: (key), assists:value};
});

var goals = _.map(goalsArray, function(value, key) {
return {player_name: (key), goals:value};
});

var combine = _.union(goals, assists);

var data = combine,
        result = function (data) {
            var r = [], o = {};
            data.forEach(function (a) {
                if (!(a.player_name in o)) {
                    o[a.player_name] = { player_name: a.player_name, goals: 0, assists: 0, total: 0 };
                    r.push(o[a.player_name]);
                }
                ['goals', 'assists'].forEach(function (k) {
                    o[a.player_name][k] += +a[k] || 0;
                    o[a.player_name].total += +a[k] || 0;
                });
            });
            return r;
        }(data);

var result = _.sortBy(result, function(o) { return o.total; });
var result = result.reverse();

var result = _.filter(result, function(item) {
return !!item && item.player_name !== "undefined" && item.player_name !== "" && item.total !== "" && item.total !== null;
});

var result = result.slice(0, 15);

return result;

@robfallows I understand your resolution now. I will use it for my application. 13h ago I could not even make a difference between cursor and array, took me two hours to figure out that I have to use .fetch() to get array that can be further processed and to be used in your method. Meteor kicks ass as I could have been building a very nice application even not understanding these basics. Appreciate all your help!

1 Like