codious
codious

Reputation: 3511

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?

Upvotes: 2

Views: 265

Answers (1)

Sebastian Blask
Sebastian Blask

Reputation: 2938

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

Upvotes: 2

Related Questions