Skip to main content
 首页 » 编程设计

python之如何对 mongodb 进行不区分大小写的正则表达式搜索

2024年12月31日31kuangbin

<分区>

我正在尝试从 mongodb 数据库中读取数据,但遇到了几个问题,有人可以提供指导吗?

  1. 遇到以下错误,如何解决?

  2. 有没有办法通过正则表达式进行不区分大小写的搜索,我有如下"\%train\%",不确定这是不是正确的方法

from pymongo import MongoClient 
import os,pymongo 
dbuser = os.environ.get('muser', 'techauto1') 
dbpass = os.environ.get('mpwd', 'techpass') 
uri = 'mongodb://{dbuser}:{dbpass}@machine.company.com:27017/techautomation'.format(**locals()) 
client = MongoClient(uri) 
db = client.techautomation.tech_build_audit 
try: 
  #db.tech_build_audit 
  myCursor = db.collection.find({"chip" : "4377","branch" : "\%train\%"}).sort({"_id":-1}) 
  print myCursor 
 
except pymongo.errors.AutoReconnect, e: 
  print e 

错误:

Traceback (most recent call last):

File "parseplist.py", line 11, in

myCursor = db.collection.find({"chip" : "4377","branch" : "%peaceB%"}).sort({"_id":-1})

File "/Library/Python/2.7/site-packages/pymongo/cursor.py", line 703, in sort keys = helpers._index_list(key_or_list, direction)

File "/Library/Python/2.7/site-packages/pymongo/helpers.py", line 52, in _index_list

raise TypeError("if no direction is specified, "

TypeError: if no direction is specified, key_or_list must be an instance of list

更新:

由于错误,我无法验证正则表达式是否有效,我该如何修复错误?