userMod2
userMod2

Reputation: 8960

MongoDB - Search string for an element from an array of words

How do i check if a string contains a list of words from an array. I know how do it with one word however how do with an array?

I've tried the following with no luck:

db.getCollection('questions').find( {$text:{$search:{$in: ['chips', 'mars']} }}  )

Any ideas would be appreciated.

Thanks

Upvotes: 0

Views: 956

Answers (1)

Alex Blex
Alex Blex

Reputation: 37038

db.getCollection('questions').find( {$text:{$search:"chips mars" }} ) to find questions with either "chips" or "mars" or both.

db.getCollection('questions').find( {$text:{$search:"\"chips\" \"mars\"" }} ) to find questions with both "chips" and "mars".

Docs for text index search reads:

$search is a string of terms that MongoDB parses and uses to query the text index. MongoDB performs a logical OR search of the terms unless specified as a phrase.

Upvotes: 3

Related Questions