今天写程序修复mongo数据库,字段JobId 类型为ObjectId 的问题(这个字段应该是存为字符串类型的mongoid)。发现已更新就报E11001 duplicate key on update .后来才发现,JobId这个字段,加了mongoid的unique类型索引,JobId 修复为字符串类型的mongoid时,会跟数据库中已经存在的这个字符串mongoid冲突。郁闷。搞了半天是这个问题.部分代码如下:抓取重复key异常,如果有异常则删除这条数据。
try:
print db.JobInfo.update( updateQuery, {"$set":updateDict} ,safe = True)
except pymongo.errors.DuplicateKeyError,e:
try:
print db.P_JobInfo.remove( updateQuery,safe = True)
except:
connectOkFlag = False
break
except Exception,e:
print str(e)
connectOkFlag = False
break
try:
print db.JobInfo.update( updateQuery, {"$set":updateDict} ,safe = True)
except pymongo.errors.DuplicateKeyError,e:
try:
print db.P_JobInfo.remove( updateQuery,safe = True)
except:
connectOkFlag = False
break
except Exception,e:
print str(e)
connectOkFlag = False
break