0

I have documents like this

{ 
  Name: "some data"
  Another: " a lots of data in string"
  Another: "string data here too
}

And have an array

Keyword = ["key1", "key2", .."keyn"]

I want to run a query such that it returns the documents if any of keyword from array matches the word from document's name or another or another or any other string value. Doesn't require exact word.

I have tried $in b return results m if the value is exactly matched.

And i have tried $search which doesn't require exact match but it won't accept array as an argument..

Is there any combine way to pass array as an argument without exact string matching.

Note: its not necessary to match all the keywords from the array. If single keywords match . It should return the results.

Thanks.

prasad_
  • 12,755
  • 2
  • 24
  • 36
  • Does this help? [MongoDB Full and Partial Text Search](https://stackoverflow.com/q/44833817/996081) – cbr May 27 '20 at 19:23
  • MongoDB provides text indexes to support [text search](https://docs.mongodb.com/manual/text-search/index.html) queries on string content. [Text Indexes](https://docs.mongodb.com/manual/core/index-text/) can include any field whose value is a string or an array of string elements. – prasad_ May 28 '20 at 01:24

1 Answers1

0

You might want to consider turning your Query into a Regex Search:

https://docs.mongodb.com/manual/reference/operator/query/regex/

so the Regex Query would be:

{
    Name: {
        $regex: /(key1|key2|keyn)/
    }
}