MongoDB聚合汇总子文档上的每个键

Irf*_*fan 4 javascript mapreduce mongodb mongodb-query aggregation-framework

我有这个架构的多个文档,每个文档是每天每个产品:

{
    _id:{},
    app_id:'DHJFK67JDSJjdasj909',
    date:'2014-08-07',
    event_count:32423,
    event_count_per_type: {
        0:322,
        10:4234,
        20:653,
        30:7562
    }
}
Run Code Online (Sandbox Code Playgroud)

我想得到特定date_range的每个event_type的总和.
这是我正在寻找的输出,其中每个事件类型已在所有文档中求和.event_count_per_type的键可以是任何东西,所以我需要一些可以遍历每个键的东西,而不是必须隐含它们的名字.

{
    app_id:'DHJFK67JDSJjdasj909',
    event_count:324236456,
    event_count_per_type: {
        0:34234222,
        10:242354,
        20:456476,
        30:56756
    }
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,我一直在尝试几个查询,这是迄今为止我得到的最好的但是子文档值没有求和:

db.events.aggregate(
{
    $match: {app_id:'DHJFK67JDSJjdasj909'}
},
{
    $group: {
        _id: {
            app_id:'$app_id',
        },
        event_count: {$sum:'$event_count'},
        event_count_per_type: {$sum:'$event_count_per_type'}
    }
},
{
    $project: {
        _id:0,
        app_id:'$_id.app_id',
        event_count:1,
        event_count_per_type:1
    }
}
)
Run Code Online (Sandbox Code Playgroud)

我看到的输出是event_count_per_type键的值为0,而不是对象.我可以修改架构,以便键是在文档的顶层,但将仍然意味着我必须组语句给每个密钥,因为我不知道密钥的名字会是什么一个条目我不能做.

任何帮助将不胜感激,如果需要,我愿意更改我的架构,并尝试mapReduce(尽管从文档中看起来表现不错.)

Nei*_*unn 7

如上所述,除非您实际上要提供所有密钥,否则使用聚合框架无法处理此类文档,例如:

db.events.aggregate([
   { "$group": {
       "_id": "$app_id",
       "event_count": { "$sum": "$event_count" },
       "0": { "$sum": "$event_count_per_type.0" },
       "10": { "$sum": "$event_count_per_type.10" }
       "20": { "$sum": "$event_count_per_type.20" }
       "30": { "$sum": "$event_count_per_type.30" }
   }}
])
Run Code Online (Sandbox Code Playgroud)

但是,您当然必须明确指定您希望处理的每个键.对于MongoDB中的聚合框架和一般查询操作都是如此,对于访问以"子文档"形式标注的元素,您需要指定元素的"确切路径"以便对其执行任何操作.

聚合框架和一般查询没有"遍历"的概念,这意味着它们不能处理文档的"每个密钥".这需要一个语言结构,以便在这些接口中不提供.

一般来说,使用"密钥名称"作为数据点,其名称实际上代表"值"是一个"反模式".对此进行建模的更好方法是使用数组并将"类型"表示为值:

{
    "app_id": "DHJFK67JDSJjdasj909",
    "date: ISODate("2014-08-07T00:00:00.000Z"),
    "event_count": 32423,
    "events": [
        { "type": 0,  "value": 322  },
        { "type": 10, "value": 4234 },
        { "type": 20, "value": 653  },
        { "type": 30, "value": 7562 }
    ]
}
Run Code Online (Sandbox Code Playgroud)

还注意到"日期"现在是一个正确的日期对象而不是字符串,这也是一个很好的做法.这种数据虽然易于使用聚合框架处理:

db.events.aggregate([
    { "$unwind": "$events" },
    { "$group": {
        "_id": { 
            "app_id": "$app_id",
            "type": "$events.type"
        },
        "event_count": { "$sum": "$event_count" },
        "value": { "$sum": "$value" }
    }},
    { "$group": {
        "_id": "$_id.app_id",
        "event_count": { "$sum": "$event_count" },
        "events": { "$push": { "type": "$_id.type", "value": "$value" } }
    }}
]) 
Run Code Online (Sandbox Code Playgroud)

这显示了一个两阶段分组,首先得到每个"类型"的总数,而不指定每个"密钥",因为你不再需要,然后每个"app_id"返回一个文档,结果在最初存储的数组中.对于查看特定范围内的某些"类型"或甚至"值",该数据形式通常更加灵活.

如果您无法更改结构,那么您唯一的选择就是mapReduce.这允许您"编码"密钥的遍历,但由于这需要JavaScript解释和执行,因此它不如聚合框架快:

db.events.mapReduce(
    function() {
        emit(
            this.app_id,
            {
                "event_count": this.event_count,
                "event_count_per_type": this.event_count_per_type
            }
        );
    },
    function(key,values) {

        var reduced = { "event_count": 0, "event_count_per_type": {} };

        values.forEach(function(value) {
            for ( var k in value.event_count_per_type ) {
                if ( !redcuced.event_count_per_type.hasOwnProperty(k) )
                    reduced.event_count_per_type[k] = 0;
                reduced.event_count_per_type += value.event_count_per_type;
            }
            reduced.event_count += value.event_count;
        })
    },
    {
        "out": { "inline": 1 }
    }
)
Run Code Online (Sandbox Code Playgroud)

这将基本上遍历并组合"键"并总结每个找到的值.

所以你可以选择:

  1. 更改结构并使用标准查询和聚合.
  2. 保持结构并需要JavaScript处理和mapReduce.

这取决于您的实际需求,但在大多数情况下,重组会带来收益.