Is there a way to specify the order of keys to sort by?
There seems to be some confusion in the comments. To be clear, I'm not asking about ascending or descending order. I'm asking if I can specify sorting priority.
For example, I want to sort by:
- pinned (Boolean)
- score (Number)
- newest (Date or ObjectId)
so that
- only docs with the same
pinned
value get sorted byscore
, and - only docs with the same
pinned
andscore
get sorted bynewest
?
In other words,
score
,newest
should never be considered ifpinned
value is different (just sort bypinned
)newest
should never be considered ifscore
is different.
For example,
{ pinned: false, score:1, _id: new }
{ pinned: true, score:1, _id: oldest }
{ pinned: false, score:2, _id: old }
should be ordered as
{ pinned: true, score: 1, _id: oldest }
{ pinned: false, score: 1, _id: new }
{ pinned: false, score: 2, _id: old }