我想从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)