本文介绍了Pymongo即使出现重复键错误也获得插入的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Flask应用程序,并将其与mongodb结合使用.在一个端点中,我获取了csv文件,然后使用 insert_many()将内容插入到mongodb中.在插入之前,我正在创建一个唯一索引以防止在mongodb上重复.当没有重复时,我可以到达该过程的 inserted_ids ,但是当它出现重复错误时,我会得到 None ,而我不会得到 inserted_ids .我也在使用 ordered = False .有什么办法允许我即使出现重复的键错误也获得 inserted_ids 吗?

I am working on a flask app and using mongodb with it. In one endpoint i took csv files and inserts the content to mongodb with insert_many() . Before inserting i am creating a unique index for preventing duplication on mongodb. When there is no duplication i can reach inserted_ids for that process but when it raises duplication error i get None and i can't get inserted_ids . I am using ordered=False also. Is there any way that allows me to get inserted_ids even with duplicate key error ?

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的详细信息.

Unfortunately, not in the way you have done it with the current pymongo drivers. As you have found, if you get errors in your insert_many() it will throw an exception and the exception detail does not contain details of the inserted_ids.

它确实包含失败密钥的详细信息(在 e.details ['writeErrors'] [] ['keyValue'] 中),因此您可以尝试从原始产品中进行反向操作列表.

It does contain details of the keys the fail (in e.details['writeErrors'][]['keyValue']) so you could try and work backwards from that from your original products list.

您的其他解决方法是在try ...循环中使用 insert_one(),并检查每个插入.我知道这样做效率较低,但这是一种解决方法...

Your other workaround is to use insert_one() in a loop with a try ... except and check each insert. I know this is less efficient but it's a workaround ...

这篇关于Pymongo即使出现重复键错误也获得插入的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 16:19