我正在尝试创建一个基于查找结果的新集合。
在mongodb(robomongo)命令行中,如果我这样做
db.liCollection.find({current_companies : { $regex: /^DIKW/i }})260万份文件中有11份我很满意。
现在,如果我尝试像这样使用pymongo:
from pymongo import MongoClient
uri = "mongodb://user:password@example.com/the_database"
client = MongoClient('pcloud')
# connect to the liDB
li_db = client['liDB']
#get all dikw employees
dikw_current = li_db.liCollection.find({'current_companies':{'$regex':'/^DIKW/i'}})
list(dikw_current)也像这样使用regex没有结果..。
import re
regx = re.compile("/^DIKW/i", re.IGNORECASE)
li_db.liCollection.find_one({"current_companies": regx})出什么事了?
发布于 2015-03-05 20:00:36
对于pymongo,您不会在正则表达式中使用斜杠作为分隔符,因为您使用的是python正则表达式。请参阅为什么
将查询更改为li_db.liCollection.find_one({"current_companies": "^DIKW"}) .If,您需要在regex中指定选项使用re.compile
import re
regx = re.compile("^DIKW", re.IGNORECASE)
li_db.liCollection.find_one({"current_companies": regx})发布于 2016-02-12 16:05:23
我刚刚发现您也可以使用$regex语法。您不需要导入re模块并使用python :只需添加$options param,它也将在pymongo上工作。
db.liCollection.find({'current_companies' : {'$regex': '^DIKW', '$options': 'i'}})来源:选项
https://stackoverflow.com/questions/28885844
复制相似问题