How use sql “like” in PyMongo?

2019-03-12 03:21发布

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条回答
beautiful°
2楼-- · 2019-03-12 03:22

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

 db.houses.find({"hid":{"$regex": u"9"}})
查看更多
登录 后发表回答