MongoDB.live, free & fully virtual. Register Now MongoDB.live, free & fully virtual. Register Now

Help with conditional sorting in aggregation

how can i use $sort and $cond thogether in aggregation.
Or is there any way to use conditional based sorting.

From the docs, $sort does not accept an expression, so whatever you’re thinking is probably not possible.

That being said, a more concrete example may help to find an alternative.

A simple solution would be to add a field first($addFields),using the condition,
and sort after on that field.

i already tried it with $set but not working fine

Ya i am trying to find an alternative

add more details to the post