How use sql “like” in PyMongo?

2019-03-12 02:53发布

问题:

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.

回答1:

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

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