Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How use sql "like" in PyMongo?

Tags:

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.

like image 453
Ruslan Sharipov Avatar asked Apr 04 '12 20:04

Ruslan Sharipov


1 Answers

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

 db.houses.find({"hid":{"$regex": u"9"}}) 
like image 98
georg Avatar answered Nov 14 '22 18:11

georg