0

I want to write a query to make the search operations on partial words but not getting any kind of solution other than Atlas Search. I have one solution to use regex but by using regex query is taking more time even after I have created index on them.

I text search method, we can only provide the complete word which is not the scenario in our case.

For eg. - {$text:{$search:'Lionel Messi'}}, it will go for Lionel and messi in the fields but,

if {$text:{$search:'ione'}}, this is the query then it is not giving any result because the word is not a complete match.

I can not use only regex here as it is very time taking so do you have any suggestions or solutions?

Is there any way to make the searching on partial text?

Thanks in Advance.

I have tried searching about this but got no success, all I can get is the solution to go for Atlas search only.

vish anand
  • 111
  • 1
  • 4

1 Answers1

0

For simple partial text search you may consider $indexOfCP , it is known to outperform $regex unless it is some case where complex $regex is necessary, example:

db.collection.aggregate([
{
 $match: {
  $expr: {
    $gt: [
      {
        $indexOfCP: [
          "$key",
          "jump"
        ]
      },
      -1
    ]
  }
 }
 }
])

or

 db.collection.find( 
      { 
       $where: "this.key.indexOf('jump') >= 0" 
      } 
 ).hint({key:1})

Playground1

Playground2

R2D2
  • 9,410
  • 2
  • 12
  • 28