Search code examples
pythonazure-functionsazure-cosmosdbupsert

upsert_item into cosmosdb via python


I have a this code:

import logging
import json
import os
from azure.cosmos import CosmosClient
import azure.functions as func

url = os.environ["ACCOUNT_URI"]
key = os.environ["ACCOUNT_KEY"]
client1 = CosmosClient(url, key)
client = CosmosClient.from_connection_string(os.environ["CosmosDBConnStr"])

database_name = ""
container_name = ""

database = client.get_database_client(database_name)
container = database.get_container_client(container_name)

logging.info(f"container: {url}")
def main(myblob: func.InputStream, doc: func.Out[func.Document]):
    logging.info(f"Python blob trigger function processed blob \n"
                 f"Name: {myblob.name}\n")
    #reading file from blob
    contract_data=myblob.read()  
    
    try:
        logging.info(f"contract data: {contract_data}")
        contract_json = json.loads(contract_data)       
        version = contract_json.get("version")
        name = contract_json.get("name")
        title = contract_json.get("title")
        logging.info(f"contract json: {contract_json}")
        query = "SELECT c.version,c.name,c.title,c.Theme,c.description,c['data owner'],c.confidentiality,c.table1 FROM c "
      
       
        items = list(container.query_items(
            query=query,
            enable_cross_partition_query=True
        ))
        logging.info(f"item: {items[0]}")
        for item in items:
            if item["name"] == name and item["version"] == version:
                if item["title"] == title:
                    logging.info(f"Skipping, item already exists: {item}")
                    return  # Skip saving the document
                
                container.upsert_item(body=contract_json,pre_trigger_include = 
                None,post_trigger_include= None)
                return

        doc.set(func.Document.from_json(contract_data))
                       
    except Exception as e:
            logging.info(f"Error: {e}")

I added one document in my cosmosdb, I would like to replace same document has different title. but i could not do that i am getting Code: BadRequest Message: Message: {"Errors":["One of the specified inputs is invalid"]} this is an example of item from my query:

{'version': 'V1', 'name': 'demo_contract2', 'title': 'title122', 'Theme': 'Theme12', 'description': 'test data contract management2', 'data owner': '[email protected]', 'confidentiality': 'open', 'table1': {'description:': 'testen', 'attribute_1': {'type': 'int', 'description:': 'testen', 'identifiability': 'identifiable'}}}

this is an example of contract_json from my file:

{'version': 'V1', 'name': 'demo_contrac1', 'title': 'title1', 'Theme': 'Theme1', 'description': 'test data contract management2', 'data owner': '[email protected]', 'confidentiality': 'open', 'table1': {'description:': 'testen', 'attribute_1': {'type': 'int', 'description:': 'testen', 'identifiability': 'identifiable'}}}

they are matching. How should I regulate my upsert_item function in my code?


Solution

  • You are missing the id in the Upsert content. A document's identity is defined by the combination of id and Partition Key value, if you don't specify the id then Upsert will always behave as a Create operation.

    Because you are getting the items through a query, just add the id:

    query = "SELECT c.id, c.version,c.name,c.title,c.Theme,c.description,c['data owner'],c.confidentiality,c.table1 FROM c "
         
    

    You can now get the id from item["id"] to use if needed. The body of the Upsert operation should contain id, and if that matches with an existing document, then the document will get updated.