Mongoose text-search with partial string : <p>Hi i'm using <strong>mongoose</strong> to search for persons in my collection.</p>

<pre><code>/*Person model*/
{
    name: {
       first: String,
       last: String
    }
}
</code></pre>

<p>Now i want to search for persons with a query:</p>

<pre><code>let regex = new RegExp(QUERY,'i');

Person.find({
   $or: [
      {'name.first': regex},
      {'name.last': regex}
   ]
}).exec(function(err,persons){
  console.log(persons);
});
</code></pre>

<p>If i search for <strong>John</strong> i get results (event if i search for <strong>Jo</strong>).
But if i search for <strong>John Doe</strong> i am not getting any results obviously.</p>

<p>If i change <strong>QUERY</strong> to <strong>John|Doe</strong> i get results, but it returns all persons who either have <strong>John</strong> or <strong>Doe</strong> in their last-/firstname.</p>

<p>The next thing was to try with mongoose textsearch:</p>

<p>First add fields to index:</p>

<pre><code>PersonSchema.index({
   name: {
      first: 'text',
      last: 'text'
   }
},{
   name: 'Personsearch index',
   weights: {
      name: {
          first : 10,
          last: 10
   }
}
});
</code></pre>

<p>Then modify the Person query:</p>

<pre><code>Person.find({ 
    $text : { 
        $search : QUERY
    } 
},
{ score:{$meta:'textScore'} })
.sort({ score : { $meta : 'textScore' } })
.exec(function(err,persons){
    console.log(persons);
});
</code></pre>

<p>This works just fine! <strong>But</strong> now it is only returning persons that match with the whole first-/lastname: </p>

<p>-> <strong>John</strong> returns value</p>

<p>-> <strong>Jo</strong> returns no value</p>

<p>Is there a way to solve this? </p>

<p>Answers <strong>without external plugins</strong> are preferred but others are wished too.</p>
