I am currently using MongoDB’s MapReduce to generate hourly ad view counts like this:
{ _id : "4/1/2011 9:00 AM", value : { AdViews_Total : 4 } }
This works fine, and I get the results in a collection that I can subsequently query much more quickly than the original data. Now, what I’d like to do is something like this:
{ _id : "4/1/2011 9:00 AM", value : { ByBrowser : { "Internet Explorer" : 4, "FireFox" : 4 } } }
To do that, I think I’d need to be able to merge two or more disjoint documents in my Reduce operation, for example:
{ _id : "4/1/2011 9:00 AM", value : { ByBrowser : { "FireFox" : 3 } } }
{ _id : "4/1/2011 9:00 AM", value : { ByBrowser : { "FireFox" : 1 } } }
{ _id : "4/1/2011 9:00 AM", value : { ByBrowser : { "Internet Explorer" : 4 } } }
Does anyone know what such a Reduce operation might look like, keeping in mind that the browser names are not known ahead of time?
I have managed to achieve what I am after using the following, though I suspect that there might be a more efficient way of doing it. I’ll leave this up for a day before marking as answer…