I am trying to sum across a field within an aggregate pipeline where the field may not exist. Otherwise, the return should be zero. This is my code so far:
admits = [
{'$match': {'meta.State': item['state'],'meta.County': item['county'], 'meta.first_seen': date}},
{'$group': {'_id': {'item': '$item'}, 'admissions': {'$ifNull': [{'$sum': 1}, 0]}}},
]
This does not work, because calling $sum
within an $ifNull
raises a unary operator exception:
pymongo.errors.OperationFailure: The $ifNull accumulator is a unary operator