I have a group stage in my pipeline like:
$group: {
_id: null,
count: {$sum: 1},
results: {$push: '$$ROOT'}
}
Sometimes $$ROOT is empty and there are no results. In theses cases, I get an empty result after the group stage which is not my desired behaviour. I want to get the below object, instead with a zero count and empty results.
[
{
count: 0,
results: []
}
]
How can I achieve this? I tried to replace the root (by replaceRoot) but it seems when there is nothing there is no root either.