moringaman
7/20/2016 - 12:45 AM

A reactive aggregation for current user averages

A reactive aggregation for current user averages

Meteor.publish("resultAverages", function (cmd){

  ReactiveAggregate(this, GameResults,

    // Pipeline
    [
      // Stage 1
      {
        $match: {
          userId: this.userId
        //username: username//'Leon D'
        }
      },

      // Stage 2
      {
        $project: {
        "username" : 1,
        "createdAt": 1,
        "rounds": 1,
        "power": 1,
        "speed": 1,
        "punches": 1,
        "month": { "$month": "$createdAt" }
        }
      },

      // Stage 3
      {
        $sort: {
          "month": 1
          }
      },

      // Stage 4
      {
        $group: {
        _id: null,
        month: {$first: "$month"},
        rounds: {$avg: "$rounds"},
        power: {$avg: "$power"},
        speed: {$avg: "$speed"},
        punches: {$avg: "$punches"}
         }
      },

      // Stage 5
      {
        $project: {
        month: "$month",
        power: "$power",
        speed: "$speed",
        rounds: "$rounds",
        punches: "$punches"
        }
      }

    ], {
    // and send the results to another collection called below

    clientCollection: "resultReport"

    // Created with 3T MongoChef, the GUI for MongoDB - http://3t.io/mongochef

  });

});