Maximilian
Maximilian

Reputation: 1375

How can I query several words in indexed fields in pymongo?

When I want to execute a indexed text search i use the following command:

text_results = db.command('text', 'foo', search=query)

I am now wondering how I can query several words. I tried already to set the query to query = ['word1', 'word2'] but that does not work.

Upvotes: 1

Views: 36

Answers (1)

chridam
chridam

Reputation: 103335

Using the the search string "word1 word2" searches for the term word1 OR the term word2:

text_results = db.command('text', 'foo', search='word1 word2')

Also, here's a quote from docs:

If the search string includes phrases, the search performs an AND with any other terms in the search string; e.g. search for "\"twinkle twinkle\" little star" searches for "twinkle twinkle" and ("little" or "star").

So to search where the field contains "word1" AND "word2", go for

text_results = db.command('text', 'foo', search="\"word1\" \"word2\"")

Upvotes: 1

Related Questions