1

I'm trying to make a groupBy on mongoDB, using cond to select to appropriate field, however, mongo is giving me: "The field '$cond' must be an accumulator object"

$group{
 "_id" : {"dateId" : "$dateId"},
 "estimated" : { "$sum" : "$estimated_records"},
 $cond: {
   if:  {"activeSelfConsumption" : {"$exists" : true}} , (I'm also not sure if this work)
   then : {"value" : { "$sum" :"$activeSelfConsumption"}}, 
   else : {"value" : { "$sum" :"$active"}} }
}
s7vr
  • 73,656
  • 11
  • 106
  • 127
ThePoog
  • 15
  • 4
  • Possible duplicate of [mongo group and count with condition](https://stackoverflow.com/questions/30169115/mongo-group-and-count-with-condition) – Ashh Aug 14 '18 at 11:29

1 Answers1

0

$cond operator is not required. Use $ifNull instead.

Something like

{"$group":{
 "_id" : {"dateId":"$dateId"},
 "estimated":{"$sum":"$estimated_records"},
 "value":{"$sum":{"$ifNull":['$activeSelfConsumption','$active']}}
}}
s7vr
  • 73,656
  • 11
  • 106
  • 127