MongoDB汇总了不同文档中两个数组中项目的数量?

时间:2015-08-22 11:53:58

标签: node.js mongodb mongoose mongodb-query aggregation-framework

这是我的MongoDB集合架构:

company: String
model: String
cons: [String] // array of tags that were marked as "cons"
pros: [String] // array of tags that were marked as "pros"

我需要聚合它,以便得到以下输出:

[{
  "_id": {
    "company": "Lenovo",
    "model": "T400"
  },
  "tags": {
    tag: "SomeTag"
    pros: 124 // number of times, "SomeTag" tag was found in "pros" array in `Lenovo T400`
    cons: 345 // number of times, "SomeTag" tag was found in "cons" array in `Lenovo T400`
  }
}...]

我尝试执行以下操作:

var aggParams = {};
aggParams.push({ $unwind: '$cons' });
aggParams.push({ $unwind: '$pros' });
aggParams.push({$group: {
  _id: {
    company: '$company',
    model: '$model',
    consTag: '$cons'
  },
  consTagCount: { $sum: 1 }
}});
aggParams.push({$group: {
  _id: {
    company: '$_id.company',
    model: '$_id.model',
    prosTag: '$pros'
  },
  prosTagCount: { $sum: 1 }
}});
aggParams.push({$group: {
  _id: {
    company:'$_id.company',
    model: '$_id.model'
  },
  tags: { $push: { tag: { $or: ['$_id.consTag', '$_id.prosTag'] }, cons: '$consTagCount', pros: '$prosTagCount'} }
}});

但我得到了以下结果:

{
  "_id": {
    "company": "Lenovo",
    "model": "T400"
  },
  "tags": [
    {
      "tag": false,
      "pros": 7
    }
  ]
}

使用aggregation执行此操作的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

是的,考虑到有多个阵列,这有点困难,如果你同时尝试两个阵列,你最终得到一个“笛卡尔条件”,其中一个arrray乘以另一个的内容。

因此,只需在开头组合数组内容,这可能表明您应该如何存储数据:

Model.aggregate(
    [
        { "$project": {
            "company": 1,
            "model": 1,
            "data": {
                "$setUnion": [
                    { "$map": {
                        "input": "$pros",
                        "as": "pro",
                        "in": {
                            "type": { "$literal": "pro" },
                            "value": "$$pro"
                        }
                    }},
                    { "$map": {
                        "input": "$cons",
                        "as": "con",
                        "in": {
                            "type": { "$literal": "con" },
                            "value": "$$con"
                        }
                    }}
                ]
            }
        }},
        { "$unwind": "$data" }
        { "$group": {
            "_id": { 
                "company": "$company",
                "model": "$model",
                "tag": "$data.value"
            },
            "pros": { 
                "$sum": { 
                    "$cond": [
                        { "$eq": [ "$data.type", "pro" ] },
                        1,
                        0
                    ]
                }
            },
            "cons": { 
                "$sum": { 
                    "$cond": [
                        { "$eq": [ "$data.type", "con" ] },
                        1,
                        0
                    ]
                }
            }
        }
    ], 
    function(err,result) {

    }
)

因此,通过第一个$project阶段,$map运算符将“type”值添加到每个数组的每个项目。这并不重要,因为所有项目都应该处理“唯一”,$setUnion运算符将每个数组“连接”成一个单数组。

如前所述,您可能应该首先以这种方式存储。

然后处理$unwind后跟$group,其中每个“专业”和“利弊”然后通过$cond进行评估,以匹配“类型”,返回{{1} }或1其中匹配分别为0$sum聚合累加器。

这为您提供了一个“逻辑匹配”,可以根据指定的分组键计算聚合操作中的每个“类型”。