0

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

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ah ok, i just need a space between words?
Yes, I have added a quote from the docs to the answer to make it crystal clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.