aggregate([
{$unwind: '$sizes'},
{
$group: {
_id: "$_id",
brand: {$first: '$brand'},
title: {$first: '$title'},
totalShoes: {
$sum: {
$cond: [{$eq: ["$sizes", []]}, 0, "$sizes.count"]
}
},
}
},
])
As you can see I use $cond inside the $sum operator and true case returns 0, but this causes $sum to skip the model from the list. I need to get something like {title: Falcon, totalShoes: 0, sizes: []} when sizes array is empty.
Here is model example.
{
"_id": "61dc771dc825e9bb0066a20a",
"title": "Falcon",
"brand": "Adidas",
"sizes": [
{
"_id": "5fbf9730f2192b42589f63b1",
"sizeValue": 41,
"count": 1
},
{
"_id": "5fbf9730f2192b42589f63b2",
"sizeValue": 44,
"count": 4
}
]
},
{
"_id": "61fabd7d9e38f5770f4f52e5",
"title": "568",
"brand": "New Balance",
"sizes": []
},