How use sql "like" in PyMongo?

Go To StackoverFlow.com

22

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.

2012-04-04 20:26
by Ruslan Sharipov


37

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

 db.houses.find({"hid":{"$regex": u"9"}})
2012-04-04 20:34
by georg
what is the syntax if 9 represented as variable(searchword)?
db.houses.find({"hid":{"$regex": usearch
word}}) .... ?? - vogash 2016-03-10 12:14
@vogash: I guess, yes. Note that mongodb uses PCRE regexes, not python's - georg 2016-03-10 18:49
it says that variable usearch_word is not define - vogash 2016-03-12 14:42
Any answer, can you send example of using parameter instead of hard coded - vogash 2016-04-06 08:55
Ads