0

In MongoDB 3.6 I have a collection of data with a text search index. It can find the longer version of a word, but not a shorter version, how can I make it find both versions?

db.test.createIndex({name: 'text', description: 'text'});
db.test.insert({name: 'MYREALLYLONGNAME', description: 'MYREALLYLONGNAME'});

db.test.find({$text: {$search: 'MYREALLYLONGNA'}});
> FINDS IT

db.test.find({$text: {$search: 'MYREALLYL'}});
> DOES NOT FIND IT
Andrew
  • 3,545
  • 4
  • 31
  • 37

1 Answers1

0

That would have to be:

db.test.find({{$text: {$search: /.*MYREALLYL.*/}})
db.test.find( { name: { $regex: /MYREALLYL/ } } )

note: mongodb uses regular expressions which are more powerful than "LIKE" in sql. With regular expressions you can create any pattern that you imagine.

For more info on regular expressions refer to this link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

https://docs.mongodb.com/v3.6/reference/operator/query/regex/