小编use*_*562的帖子

获取MongoDB聚合$ group的百分比

我想从MongoDB聚合中的组管道中获取百分比.

我的数据:

{
    _id : 1,
    name : 'hello',
    type : 'big'
},
{
    _id : 2,
    name : 'bonjour',
    type : 'big'
},
{
    _id : 3,
    name : 'hi',
    type : 'short'
},
{
    _id : 4,
    name : 'salut',
    type : 'short'
},
{
    _id : 5,
    name : 'ola',
    type : 'short'
}
Run Code Online (Sandbox Code Playgroud)

我的请求组按类型和计数:

[{
    $group : {
        _id : {
            type : '$type'
        },
        "count" : {
            "$sum" : 1
        }
    }
}]
Run Code Online (Sandbox Code Playgroud)

结果:

[ …
Run Code Online (Sandbox Code Playgroud)

mongodb aggregation-framework

14
推荐指数
2
解决办法
1万
查看次数

标签 统计

aggregation-framework ×1

mongodb ×1