简体   繁体   English

合并MongoDB MapReduce输出

[英]Merging MongoDB MapReduce output

I am currently using MongoDB's MapReduce to generate hourly ad view counts like this: 我目前正在使用MongoDB的MapReduce来生成每小时的广告观看次数,如下所示:

{ _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: 为此,我认为我需要能够在我的Reduce操作中合并两个或更多个不相交的文档,例如:

{ _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? 有没有人知道这样的Reduce操作会是什么样子,请牢记浏览器名称是事先未知的?

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... 在标记为答案之前,我将保留一天的时间...

function Reduce(key, arr_values) {
    var reduced = { 
        AdViews_Total : 0, 
        DefaultAdViews_Total : 0, 
        Sessions_Total : 0,
        Browsers : [ ],
        }; //a document
    for(var i in arr_values) {
        reduced.AdViews_Total += isNaN(arr_values[i].AdViews_Total) ? 0 : arr_values[i].AdViews_Total;
        reduced.DefaultAdViews_Total += isNaN(arr_values[i].DefaultAdViews_Total) ? 0 : arr_values[i].DefaultAdViews_Total;
        reduced.Sessions_Total += isNaN(arr_values[i].Sessions_Total) ? 0 : arr_values[i].Sessions_Total;
        if (null != arr_values[i].Browsers)
            for (var j in arr_values[i].Browsers)
            {
                var browser = arr_values[i].Browsers[j]
                var browserLabel = browser.Browser;
                var browserCount = browser.Count;
                var browserFound = false;
                for (var k in reduced.Browsers)
                {
                    if (reduced.Browsers[k].Browser == browserLabel)
                    {
                        reduced.Browsers[k].Count += browserCount;
                        browserFound = true;
                    }
                }
                if (!browserFound)
                    reduced.Browsers[0] = browser;
            }
    }
    return reduced;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM