I figured out how to create, read, and update documents in my CosmosDB instance using Azure Cloud Functions but still have no idea how to implement delete. The main documentation I used as a reference was https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-cosmos-db-triggered-function.
I tried using both an input and output binding without an id (thus fetching the entire collection) and then filtering the item I want to remove from the inputDocuments and setting the result as the outputDocuments. Unfortunately, that doesn't seem to actually delete the item from the database instance. Any idea what I might be missing? What's the correct way to do this?
Here is the code I wrote:
const { id } = req.query;
context.bindings.outputDocuments = context.bindings.inputDocuments;
const index = context.bindings.outputDocuments.findIndex(doc => doc.id === id);
const doc = context.bindings.outputDocuments.splice(index, 1);
I also tried a simpler version but that didn't make a difference:
const { id } = req.query;
context.bindings.outputDocuments = context.bindings.inputDocuments.filter(doc => doc.id !== id);
I verified using logging statements that my function was correctly updating the outputDocuments for both of the above implementations.
Here are my bindings:
{
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"delete"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"type": "documentDB",
"name": "inputDocuments",
"databaseName": "heroesDatabase",
"collectionName": "HeroesCollection",
"connection": "ydogandjiev-documentdb_DOCUMENTDB",
"direction": "in"
},
{
"type": "documentDB",
"name": "outputDocuments",
"databaseName": "heroesDatabase",
"collectionName": "HeroesCollection",
"createIfNotExists": false,
"connection": "ydogandjiev-documentdb_DOCUMENTDB",
"direction": "out"
}
],
"disabled": false
}