0

Good morning, SO, I am currently investigating using MongoDB MapReduce functions.

My dataset looks like this

[{
     "Name" : "Person 1",
    "RunningSpeed" : [{
            "Date" : ISODate("2005-07-23T23:00:00.000Z"),
            "Value" : 10
        }, {
            "Date" : ISODate("2006-07-23T23:00:00.000Z"),
            "Value" : 20
        }, {
            "Date" : ISODate("2007-07-23T23:00:00.000Z"),
            "Value" : 30
        }, {
            "Date" : ISODate("2008-07-23T23:00:00.000Z"),
            "Value" : 40
        }

    ]

}, {
    "Name" : "Person 2",
    "RunningSpeed" : [{
            "Date" : ISODate("2005-07-23T23:00:00.000Z"),
            "Value" : 5
        }, {
            "Date" : ISODate("2006-07-23T23:00:00.000Z"),
            "Value" : 10
        }, {
            "Date" : ISODate("2007-07-23T23:00:00.000Z"),
            "Value" : 20
        }, {
            "Date" : ISODate("2008-07-23T23:00:00.000Z"),
            "Value" : 40
        }

    ]

}, {
    "Name" : "Person 3",
    "RunningSpeed" : [{
            "Date" : ISODate("2005-07-23T23:00:00.000Z"),
            "Value" : 20
        }, {
            "Date" : ISODate("2006-07-23T23:00:00.000Z"),
            "Value" : 10
        }, {
            "Date" : ISODate("2007-07-23T23:00:00.000Z"),
            "Value" : 30
        }, {
            "Date" : ISODate("2008-07-23T23:00:00.000Z"),
            "Value" : 25
        }

    ]

}
]

I would like to calculate the delta (or change) over each year.

My map function is

function map() {
   this.RunningSpeed.forEach(function(data){
      var year = data.Date.getFullYear();
      emit(year, // Or put a GROUP BY key here, the group by key is Date.getFullYear()
          {sum: data.Value, // the field you want stats for
           min: data.Value,
           max: data.Value,
           count: 1,

          });
   });
}

and my reduce function only returns delta accross current year, which is understood as the set is grouped by year.

reduce : function reduce(key, values) {
var a = values[0]; // will reduce into here
  for (var i=1/*!*/; i < values.length; i++){
      var b = values[i]; // will merge 'b' into 'a'


    // temp helpers
    var delta = a.sum - b.sum; // a.mean - b.mean


    // do the reducing
    a.diff = delta;

}

return a; 
}

So once again how can I adapt the previous function to generate the delta for each year calculated from the year before?

7
  • Forgive my ignorance, but the delta denotes the overall changes, right. So when it starts with 10, rises to 25 and drops to 5, delta is 20? Nov 18, 2014 at 10:00
  • Looking for the delta between years. So if it starts at 10 then the next year is 25 the delta is 15. Then it would calculate the delta from the year 2 to year 3. 25 to 5 would be -20.
    – N00b3eva
    Nov 18, 2014 at 10:02
  • Sure. But do you want the delta for all years or for two given years? Nov 18, 2014 at 10:09
  • Calculated between years, but over every year. Does that make sense?
    – N00b3eva
    Nov 18, 2014 at 10:11
  • Not really, since the sum of the positive and negative deltas would simply evaluate to ($last-$first). What would make sense is to calculate the change between two years and to sum that up. Nov 18, 2014 at 11:25

1 Answer 1

0

As per your comments and my suggestion, this m/r gives you both. Please read the comments for clarification.

db.runners.mapReduce(

  // Map
  function(){
    for(var i =0; i < this.RunningSpeed.length; i++){
      var value={
        date: this.RunningSpeed[i].Date,
        speed: this.RunningSpeed[i].Value};

      // We emit all in single key value pairs
      emit(this.Name,value);
    }
  },

  // Reduce
  function(key,values){

    //In the beginning, delta is null
    var delta=0;

    // And we start to compare with the first value
    var last=values[0].speed;

    for(var idx=1; idx < values.length; idx++){

      // The absolute delta (all changed over all years)
      delta += Math.abs( values[idx].speed - last );

      // I think here was your problem.
      // You needed to save "last" years value for comparison with the current value.
      last=values[idx].speed;
    }

    var reduced = {

      // The relative delta (speed gain and loss over all years)
      delta_rel:values[values.length-1].speed - values[0].speed,

      delta_abs:delta
    };

    return reduced;

  },
  // Options
  {
     // Output collection
     out:"runner_test",
     // Sort order of the initial documents
     sort:{"RunningSpeed.Date":1}}
)
1
  • Thank you for your solution. However not to sure if we were misunderstood but what I was looking for is the delta calculated year on year. Currently this gives me the delta across all years
    – N00b3eva
    Dec 2, 2014 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.