title: Sort by keyword index mongodb
body: <p>I was wondering if there was a way to sort my mongodb results by the index of the search keyword.
  <br>
  For example if I search the db by <code>{name: da}</code> i'll get back results like <code>[{name: &quot;yoda&quot;}, {name: &quot;david&quot;}, {name: &quot;soda&quot;}, {name: &quot;daniel&quot;}]</code>. But I would want the order to be something like this <code>[{name: &quot;david&quot;}, {name: &quot;daniel&quot;}, {name: &quot;yoda&quot;}, {name: &quot;soda&quot;}]</code>. I know that I can sort the results after I find them like this:</p>
  <blockquote>
  <p><code>const re = new RegExp(keyword, &quot;i&quot;);</code><br>
  <code>results.sort((a, b) =&gt; a.name.search(re) - b.name.search(re)</code></p>
  </blockquote>
  <p>But I also wanted to limit the amount of documents I get back from the database. I looked at the textScore metadata in the mongodb docs, but I have no clue if its useful in my case.</p>