1

The answer here applies when using PyMongo to search fields containing a search text. But only for hard-coded string values:

 db.houses.find({"hid":{"$regex": u"9"}})

So, how to shape that expression replacing that "9" with a variable?

def some_func(search_text)
    db.houses.find({"hid":{"$regex": ??????}})
2
  • 2
    are you stating that db.houses.find({"hid":{"$regex": search_text}}) won't work? Commented Jul 18, 2019 at 10:47
  • The linked answer had comments looking for that kind of implementation. So, this question is for the ones hitting the end of the road on that question. Commented Jul 18, 2019 at 10:58

2 Answers 2

2

Use re module to create a regex for your search string

import re
search_string = re.compile('9')
db.houses.find({'hid': search_string})

you can use a lot of options available in re module

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

Comments

1

Try this,

search_text = u"9"
db.houses.find({"hid":{"$regex": search_text }})

2 Comments

Ok, my first example confused you. Putting search_text that way is not always possible.
@vahdet From comments provided from linked answer. it's '9' where u'9' should work.

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.