如何使用sql“like"?在 PyMongo 中?
问题描述
如何在 PyMongo 中使用 sql like"?
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
文档说 sql 喜欢"(SELECT * FROM users WHERE name LIKE "%Joe%"
) 在 MongoDB 中是 db.users.find ({name:/Joe/})
.
The documentation says that sql "like" (SELECT * FROM users WHERE name LIKE "%Joe%"
) in MongoDB is db.users.find ({name:/Joe/})
.
如果直接向cli-client接口mongodb指定查询,那么一切正常,但在pymongo中不行.
If you specify a query directly to the cli-client interface mongodb, then everything works correctly, but does not work in pymongo.
有什么问题?
谢谢.
解决方案
pymongo 不支持正则表达式,你必须使用 '$regex' 谓词:
pymongo doesn't support regex literals, you have to use the '$regex' predicate:
db.houses.find({"hid":{"$regex": u"9"}})
相关文章