Pymongo 即使有重复键错误也能插入 id
Pymongo get inserted id's even with duplicate key error
我正在开发一个烧瓶应用程序并使用 mongodb。在一个端点中,我获取了 csv 文件并将内容插入 mongodb 和 insert_many()
。在插入之前,我正在创建一个唯一索引以防止 mongodb 上的重复。当没有重复时,我可以为该进程达到 inserted_ids
但是当它引发重复错误时,我得到 None
而我无法得到 inserted_ids
。我也在使用 ordered=False
。有什么方法可以让我得到 inserted_ids
即使出现重复键错误?
def createBulk(): #in controller
identity = get_jwt_identity()
try:
csv_file = request.files['csv']
insertedResult = ProductService(identity).create_product_bulk(csv_file)
print(insertedResult) # this result is None when get Duplicate Key Error
threading.Thread(target=ProductService(identity).sendInsertedItemsToEventCollector,args=(insertedResult,)).start()
return json_response(True,status=200)
except Exception as e:
print("insertedResultErr -> ",str(e))
return json_response({'error':str(e)},400)
def create_product_bulk(self,products): # in service
data_frame = read_csv(products)
data_json = data_frame.to_json(orient="records",force_ascii=False)
try:
return self.repo_client.create_bulk(loads(data_json))
except bulkErr as e:
print(str(e))
pass
except DuplicateKeyError as e:
print(str(e))
pass
def create_bulk(self, products): # in repo
self.checkCollectionName()
self.db.get_collection(name=self.collection_name).create_index('barcode',unique=True)
return self.db.get_collection(name=self.collection_name).insert_many(products,ordered=False)
不幸的是,不是你用当前的 pymongo 驱动程序完成的方式。正如您所发现的,如果您在 insert_many()
中遇到错误,它将引发异常并且异常详细信息不包含 inserted_id
s.
的详细信息
它确实包含失败密钥的详细信息(在 e.details['writeErrors'][]['keyValue']
中),因此您可以尝试从原始产品列表中逆向计算。
您的另一个解决方法是在循环中使用 insert_one()
并尝试...除了并检查每个插入。我知道这效率较低,但这是一种解决方法...
我正在开发一个烧瓶应用程序并使用 mongodb。在一个端点中,我获取了 csv 文件并将内容插入 mongodb 和 insert_many()
。在插入之前,我正在创建一个唯一索引以防止 mongodb 上的重复。当没有重复时,我可以为该进程达到 inserted_ids
但是当它引发重复错误时,我得到 None
而我无法得到 inserted_ids
。我也在使用 ordered=False
。有什么方法可以让我得到 inserted_ids
即使出现重复键错误?
def createBulk(): #in controller
identity = get_jwt_identity()
try:
csv_file = request.files['csv']
insertedResult = ProductService(identity).create_product_bulk(csv_file)
print(insertedResult) # this result is None when get Duplicate Key Error
threading.Thread(target=ProductService(identity).sendInsertedItemsToEventCollector,args=(insertedResult,)).start()
return json_response(True,status=200)
except Exception as e:
print("insertedResultErr -> ",str(e))
return json_response({'error':str(e)},400)
def create_product_bulk(self,products): # in service
data_frame = read_csv(products)
data_json = data_frame.to_json(orient="records",force_ascii=False)
try:
return self.repo_client.create_bulk(loads(data_json))
except bulkErr as e:
print(str(e))
pass
except DuplicateKeyError as e:
print(str(e))
pass
def create_bulk(self, products): # in repo
self.checkCollectionName()
self.db.get_collection(name=self.collection_name).create_index('barcode',unique=True)
return self.db.get_collection(name=self.collection_name).insert_many(products,ordered=False)
不幸的是,不是你用当前的 pymongo 驱动程序完成的方式。正如您所发现的,如果您在 insert_many()
中遇到错误,它将引发异常并且异常详细信息不包含 inserted_id
s.
它确实包含失败密钥的详细信息(在 e.details['writeErrors'][]['keyValue']
中),因此您可以尝试从原始产品列表中逆向计算。
您的另一个解决方法是在循环中使用 insert_one()
并尝试...除了并检查每个插入。我知道这效率较低,但这是一种解决方法...