我想使用 pymongo 进行正则表达式 mongodb 查询。
cond = {'date':'/.*2011-8-11.*/'}
coll.find(cond).count() return 0;
但我直接在 Mongodb 上执行此查询 return 25; 我的查询有问题吗?
最佳答案
要使用 pymongo 的正则表达式进行搜索,您需要使用 python 正则表达式对象,而不是带斜杠的字符串。对于上面的查询,pymongo 语法为:
import re
# assume connection is set up, and db
# is a pymongo.database.Database instance
date_re = re.compile(r'2011-8-11')
db.collection.find({'date': date_re})
另请注意,您不需要 .
字符(在 pymongo 语法或 mongo shell 语法中),除非您使用 ^
或$
。
关于python - 如何使用 pymongo 在 Mongodb 中进行正则表达式搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7024137/