import pymongo
from utils.config import MONG_CONN
jiastondb = MONG_CONN['wilsoner']
jiastonbooklistcol = jiastondb["wilsonuser"]
for item in jiastonbooklistcol.find():
print(item)
try:
jiastonbooklistcol.insert({'name': 'wilsoner', 'age': 36, 'sex': 'female'})
except Exception as e :
print(e)
except pymongo.errors.DuplicateKeyError:
print('--')
finally:
print('*************')
E11000 duplicate key error collection: wilsoner.wilsonuser index: name_1_age_1 dup key: { : “caocao”, : 36 }
当pymongo创建唯一性索引后,再次插入唯一键重复的数据的时候,会报错,最好在Exception之前对重复的键进行处理。