Mongodb聚合:明显计数

时间:2017-12-01 17:01:42

标签: mongodb aggregation-framework

我对聚合框架相当新,并且有点卡住了。我希望有人可以给我一些建议。我已将数据纠缠在以下表格中:

{ 
    "python" : "2.7.9", 
    "ACmanagerversion" : "TBD", 
    "ACstatusmanager" : "0.13", 
    "ACversion" : "0.9.2"
}
{ 
    "python" : "2.7.9", 
    "ACmanagerversion" : "TBD", 
    "ACstatusmanager" : "0.16", 
    "ACversion" : "0.9.1"
}
{ 
    "python" : "2.7.9", 
    "ACmanagerversion" : "1.1", 
    "ACstatusmanager" : "0.16", 
    "ACversion" : "0.9.1"
}
{ 
    "python" : "2.7.4", 
    "ACmanagerversion" : "1.3", 
    "ACstatusmanager" : "0.16", 
    "ACversion" : "0.9.1"
}

我想返回表格(或类似)的数据:

{
"type" : "python",
"2.7.9" : 3,
"2.7.4" : 1
},
{
"type" : "ACversion",
"0.9.1" : 3,
"0.9.2" : 1
}
...

(即计算所有独特的事件)

通过运行:

{
    $group: {
        "_id" : "$python", version_count: { $sum: 1 }
    }
}

我得到了

{ 
    "_id" : "2.7.6", 
    "version_count" : 1.0
}
{ 
    "_id" : "2.7.9", 
    "version_count" : 36.0
}

我只是变得有点陷入困境,变成了一个更通用的'解决方案,我自动计算出各种软件,而不必为每种类型指定一个。

0 个答案:

没有答案