Return 0 if $sum is null (aggregation framework)

天涯浪子 提交于 2019-12-23 00:54:11

问题


So I'm using aggregation framework to sum some results in given time blocks. My question is how do I return count: 0 every time if there was no results in the time block.

So let's say in first ten seconds my count: 7 in next ten seconds count: 2 and then if in the third time block there was nothing I would like aggregate function to return count: 0

Below simple summing operation:

$group: {
  _id: 1,
  count: { $sum: 1 }
}

I have tried using $ifNull, but it didn't work for me:

$group: {
  _id: 1,
  count: { $sum: { $ifNull: [ $count, 0 ] } }
}

来源:https://stackoverflow.com/questions/24955292/return-0-if-sum-is-null-aggregation-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!