Skip to content
Advertisement

Map Reduce on timestamps

Mongodb Database:

{"thread": "abc", "message": "hjhjh", "Date": (2010,4,5,0,0,0)}
{"thread": "abc", "message": "hjhjh",  "Date": (2009,3,5,0,0,0)}
{"thread": "efg", "message": "hjhjh",  "Date": (2010,3,7,0,0,0)}
{"thread": "efg", "message": "hjhjh",  "Date": (2011,4,5,0,0,0)}

How can I Map-Reduce or aggregate on the above data to generate an output as:

{"thread": "abc", "messages_per_month": 5}
{"thread": "efg", "messages_per_month": 4}

I am trying to write some code but its difficult to average on unsorted dates. Are there any built in functions averaging on time to do this?

Advertisement

Answer

You could do min/max on the dates like described here for numbers and just add a counter for the messages. The result would be a list of threads with the number of messages and min/max date. From the dates you can calculate the number of months which you can then use to average on the number of messages.

Edit: updated link with link to archived version

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement