Short one: I need to regex-search a collection, move perfect matches to first position of the result and then limit that result.
Long one:
In a publication I need to custom sort my results and then (of course after sorting) limit it.
My custom sort is something like sorting by relevance but not exactly. Given these documents:
{ number: '2123' } // let's call this one A
{ number: '21' } // and this one B
{ number: 'A2123' } // and this one C
{ number: '01' } // and this one D
If I'm searching with the term 21 I want to get A, B and C. BUT at first position B. Why that? Because B matches perfect. You see? It's not exactly sorting by relevance but moving a perfect match (there will only be 1 perfect match at most) to first position.
I tried text indexes but they don't match 2123 with the search term 21. (I understand why and that's fine)
So what exactly is my problem? I can regex-search and publish that cursor but it will have too many documents. I could limit it but without sorting I may loose perfect matches which I absolutely need.
So in the end I can't custom sort. And then I had that idea: Why don't I fetch() my cursor (unsorted and unlimited), sort and limit that fetched array and create an "artificial" cursor based on that sorted array. I didn't find a way to achieve that.