0

I am trying to apply a map reduce function in a document like this:

{
"_id": 2,
"timestamp_day": "1/20/2005 12:00:00 AM",
"variableID": "GUID",
"subscriptionID": "1",
"unit": "kWh",
"timezone": "UTC",
"sum": 0,
"numberOfRecords": 96,
"values": {
    "1": 0.06232154558520836,
    "2": 0.06232154558520836,
    "3": 0.06232154558520836,
    "4": 0.06232154558520836,
    "5": 0.06232154558520836,
    "6": 0.06232154558520836,
    "7": 0.06232154558520836,
    "8": 0.06232154558520836,
    "9": 0.06232154558520836,
    ...
   }
}

I have documents for each day I'm keeping information, and each value represents a reading. My objective is to apply a mapReduce function in this document so I can retrieve metrics like avg per day, avg per day per id, and so on. The thing is that I don't know how to do that since the values key contains a BSONElement.

So far my map function is as follows:

var mapFunction = function() {
   emit(this.subscriptionID, this.values);
}

And by what I understood with this, it will be sent to reduce function all the values stored in values, but again, since it is a BSON element how can I make those calculations? A foreach going through each one? Performance is important..

Thanks in advance.

Jorge Lima
  • 157
  • 15

1 Answers1

0

So I got a few days ago and I'll leave it here if anyone needs this.

var mapFunction = function() {
  for (var key in this.values) {
    emit(this.subscriptionID, this.values[key]);
  }
}

In this particular case this function will send to the reduce function all the values mapped to one subscriptionID as an array. For the values in the question, the map will send something like this:

[0.06232154558520836, 0.06232154558520836, 0.06232154558520836, ...]

Hope it helps someone.

Jorge Lima
  • 157
  • 15