用户登录
用户注册

分享至

如何使用sql“like"?在 PyMongo 中?

  • 作者: 123456亲亲
  • 来源: 51数据库
  • 2023-01-03

问题描述

如何在 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"}})
软件
前端设计
程序设计
Java相关