How to use upsert list of dictionaries using only beanie library in python?

Viewed 187

I am using beanie as a wrapper class to do the basic crud functionality in the Mongodb database. I am posting my code below:

async def create(lv_id, collection_name, data):
    try:
        obj = Collections.COLlECTION_DICT[collection_name]
        document = await obj.find_one(obj.lv_id == lv_id)
        if document:
            await document.update(data)
        else:
            await document.insert(data)
        return "Success", 200
    except SaveException as e:
        logging.error(e.message)
        return e.message, 500

This is the code that i am trying to post data using the curl command with asgi server:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://0.0.0.0:9200/v1/create --data '{"lv_id":"b7f6883e2cd946fd8a47a0b71d2c32fd", "collection":"errors", data:"{"_id":"b7f6883e2cd946fd8a47a0b71d2c32fd","lv_id":"b7f6883e2cd946fd8a47a0b71d2c32fd","output":[[{"oz":"02.01.","row_id":19,"process":"geruste","traceback":"Traceback (most recent call last):\n File "./lv_services/inference.py", line 172, in inference_helper\n merged = cls.collapse(tags)\n File "./lv_services/inference.py", line 228, in collapse\n elif tag == "I-" + current_entity:\nTypeError: can only concatenate str (not "NoneType") to str\n"}]]}"}'

My pydantic model is given below

class errors(Document): """ Errors model """ id: str = Field(..., alias='_id') lv_id: str output: Optional[List[List[Error]]] = []

So i want to update or insert this data in the collection named errors! But the beanie needs some key to SET the value! I just want to upsert the dictionary based on the lv_id. I want to make this code as generic as possible because i want to update other collections too with the same code. Can anyone help me in this regard? Thanks in advance

0 Answers
Related