MongoDB聚合框架$group算子自定义函数

Customized function in $group operator of aggregation framework in MongoDB

我想使用一个自定义的 $sum(我们称之为 $boolSum)函数,它 returns 数组中真实元素的数量。例如

group :{
          _id: {
                  a : '$a'
                  b : '$b',
                  c : '$c'
               },
              d1: { $boolSum : '$d1'}
              d2: { $boolSum : '$d2'}
        }

但是好像没有办法定义boolSum函数。我试图在 system.js 中添加一条新记录,但它不起作用。

在MongoDB中,是否可以在聚合框架的$group算子中自定义函数?

is it possible to cutomize function in $group operator of aggregation framework

不,不是真的...

I want to use a customized $sum (lets call it $boolSum) function that returns the number of true element in an array

...但您可以在特定情况下组合 $sum with other operators (like $cond)以获得所需的结果:

{$sum: {$cond:["$someBooleanField",1,0]}}

{$sum: {$cond:[{$eq: ["$somePossiblyBooleanField",true]},1,0]}}