How use sql "like" in PyMongo?

Ruslan Sharipov picture Ruslan Sharipov · Apr 4, 2012 · Viewed 12.2k times · Source

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.

Answer

georg picture georg · Apr 4, 2012

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

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