Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Yes, it is possible to use ifNull to handle nulls/undefined as if they were 0s during aggregation. The syntax for using ifNull in aggregation is:

{$group: {
  _id: "$field",
  sum: {$sum: {$ifNull: ["$amount", 0]}}
}}

In the example above, if the value of the field "amount" is null or undefined, it will be treated as 0 for the purpose of calculating the sum.