26

How use sql "like" in PyMongo?

>>> db.houses.find().count()
11616
>>> db.houses.find({"hid":u"16999"}).count()
1
>>> db.houses.find({"hid":u"/9/"}).count()
0

The documentation says that sql "like" (SELECT * FROM users WHERE name LIKE "%Joe%") in MongoDB is db.users.find ({name:/Joe/}).

If you specify a query directly to the cli-client interface mongodb, then everything works correctly, but does not work in pymongo.

What is the problem?

Thanks.

0

1 Answer 1

43

pymongo doesn't support regex literals, you have to use the '$regex' predicate:

 db.houses.find({"hid":{"$regex": u"9"}})
Sign up to request clarification or add additional context in comments.

4 Comments

what is the syntax if 9 represented as variable(search_word)? db.houses.find({"hid":{"$regex": usearch_word}}) .... ???
@vogash: I guess, yes. Note that mongodb uses PCRE regexes, not python's.
it says that variable usearch_word is not defined
Any answer, can you send example of using parameter instead of hard coded 9

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.