I recently created a mongo shell script to populate a new collection with the results of an aggregation. However, I encountered an issue where null values from the source collection get replaced by zeroes. The aggregation part of my script is as follows:
db.getCollection("unhcr_pop_concern_flat").aggregate(
[
{
"$group" : {
"_id" : {
"country" : "$country",
"time" : "$time",
"origin": "$origin"
},
"value_fields": {
"$addToSet": {
k: "$type",
v: {$ifNull: ["$value", null]}
}
}
}
},
{ $addFields: { value_fields: { $arrayToObject: "$value_fields" } } }
],
{
"allowDiskUse" : true
}
Can someone help me identify the problem here? I have already tried using the $ifnull operator.