Now after some time and research, I want to follow up and post our solution here. We ended up using the built-in $top
accumulator (docs):
{
$top:
{
sortBy: { <field1>: <sort order>, <field2>: <sort order> ... },
output: <expression>
}
}
We used sortBy
to sort by the absolute of the field's value, maintaining the original sign in the output
. For us, this gave us significant performance increase over the original ideas I posted.