0

I am able to write a file to cosmos DB with the help of output binding, but what I need is to know how to overwrite the existing file that is already in cosmos DB

My code looks like this

import azure.functions as func

def main(req: func.HttpRequest, doc: func.Out[func.Document]) -> func.HttpResponse:

    request_body = req.get_body()

    doc.set(func.Document.from_json(request_body))

    return 'OK'

and my output binding looks like this

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "cosmosDB",
      "direction": "out",
      "name": "doc",
      "databaseName": "demodb",
      "collectionName": "data",
      "createIfNotExists": "true",
      "connectionStringSetting": "AzureCosmosDBConnectionString"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
  ]
}

All I want is to know how could I overwrite the existing file that is cosmos DB

Please help me with some sample code.... thanks.

Stark
  • 1
  • 2

1 Answers1

0

I am able to write a file to cosmos DB with the help of output binding, but what I need is to know how to overwrite the existing file that is already in cosmos DB

  • As far as I know, currently there is no possible way to overwrite the existing file in a cosmos DB.
  • If you want to update existing document in cosmos DB, you need to query (or read), modify, and then replace the document.
  • you can refer the SDK for azure cosmos db.

I have followed this blog created by Evan Wong and I am able to append the file details.

def  main(req: func.HttpRequest, cosmos:func.DocumentList) -> func.HttpResponse:

logging.info('Python HTTP trigger function processed a request.')

User_details = []
for  user  in  cosmos:

User_details = {

"id": user['id'],

"name": user['name']

}

users_json.append(User_details)

return  func.HttpResponse(

json.dumps(User_details),

status_code=200,

mimetype="application/json"

)
Pravallika KV
  • 2,415
  • 2
  • 2
  • 7