【问题标题】:MongoDB Aggregate Sum Each Key on a SubdocumentMongoDB汇总子文档上的每个键
【发布时间】:2014-09-30 23:40:18
【问题描述】:

我有多个具有此架构的文档,每个文档每天针对每个产品:

{
    _id:{},
    app_id:'DHJFK67JDSJjdasj909',
    date:'2014-08-07',
    event_count:32423,
    event_count_per_type: {
        0:322,
        10:4234,
        20:653,
        30:7562
    }
}

我想获取特定日期范围内每个 event_type 的总和。
这是我正在寻找的输出,其中每个事件类型已在所有文档中求和。 event_count_per_type 的键可以是任何东西,所以我需要一些可以循环遍历它们的东西,而不必隐含它们的名称。

{
    app_id:'DHJFK67JDSJjdasj909',
    event_count:324236456,
    event_count_per_type: {
        0:34234222,
        10:242354,
        20:456476,
        30:56756
    }
}

到目前为止,我已经尝试了几个查询,这是迄今为止我得到的最好的,但是子文档值没有相加:

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
    }
}
)

我看到的输出是 event_count_per_type 键的值 0,而不是对象。我可以修改架构,使键位于文档的顶层,但这仍然意味着我需要在每个键的组语句中都有一个条目,因为我不知道键名是什么,所以我不能做。

任何帮助都将不胜感激,如果需要,我愿意更改我的架构并尝试使用 mapReduce(尽管从文档看来性能很差。)

【问题讨论】:

  • 如果您不知道子文档中的键名,那么您不能通过聚合来执行此操作(但您可以使用 map-reduce)。您确定键名可以是 anything 吗?还是只能是一定范围内的数字?可以是“富”吗?还是只能是 0 到 59 之间的数字或类似的数字?
  • @AsyaKamsky 它将是 0 到 100 之间的整数

标签: javascript mongodb mapreduce mongodb-query aggregation-framework


【解决方案1】:

如上所述,使用聚合框架处理这样的文档是不可能的,除非您实际上要提供所有键,例如:

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" }
   }}
])

但您当然必须明确指定您希望处理的每个键。 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 }
    ]
}

还要注意“日期”现在是一个正确的日期对象而不是一个字符串,这也是一个很好的做法。这种数据虽然很容易使用聚合框架进行处理:

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" } }
    }}
]) 

这显示了一个两阶段分组,首先获取每个“类型”的总数,而不指定每个“键”,因为您不再需要指定每个“键”,然后作为每个“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 }
    }
)

这实际上将遍历并组合“键”,并对找到的每个键的值求和。

所以你的选择是:

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

这取决于您的实际需求,但在大多数情况下,重组会产生好处。

【讨论】:

  • 谢谢,太好了!我仍处于此应用程序的早期阶段,因此我将继续更改架构。我实际上有另一个我想同时合并的数组,称为 unqiue_event_count_per_type,但是当我将第二个 $unwind 包含在第一个 $group _id 中时,第二个 $unwind 导致 $sum 对记录进行双重计数。我是否必须将其作为第二个查询并将两个结果合并到代码中?
  • @Irfan 这听起来像是另一个问题,最好通过发布一个包含所有细节的新问题来表达。不要忘记接受对您有帮助的答案,因为我们中的一些人会查看您的历史中是否有接受的答案。合并两个数组是可能的,但整个问题值得另一个完整的答案。
  • 谢谢,我在stackoverflow.com/questions/25201157/…创建了一个新问题
猜你喜欢
  • 2013-07-13
  • 2018-06-06
  • 2018-10-27
  • 1970-01-01
  • 2015-04-01
  • 2015-04-03
  • 2015-11-16
  • 1970-01-01
  • 2014-04-01
相关资源
最近更新 更多