I have created a collection in cosmosDB with a unique key. Ideally collection should be updated with new values if an existing key is entered as input for the collection. I have an Azure cosmosDB trigger function which is configured the above collection as output.
Below is the index.js file which i am having the logic implementation.
module.exports = async function (context, documents) {
var StatusInput = context.bindings.StatusInput; //additional input
if (!!documents && documents.length > 0) {
var finalOutput = [];
// logic implementation
for(var i = 0; i < documents.length; i++){
var document = documents[i];
var baseID = document.id;
baseTempJson = {};
var abcValue = null;
var xyzValue = null;
const checkForID = obj => obj.id === baseID;
if(!(StatusInput.some(checkForID))){
if(!!document.abc && document.abc !=null) {
abcValue = document.abc;
} if(!!document.xyz && document.xyz != null) {
xyzValue = document.xyz;
}
baseTempJson = {"id": baseID, "abc": abcValue, "xyz": xyzValue};
finalOutput.push(baseTempJson);
} else
{
StatusInput.forEach(function(element){
var innerID = element.id;
var tempJson = {};
var abcValue = null;
var xyzValue = null;
if(innerID == baseID){
context.log('Data for the ID ' + innerID + ' is existing. Updating the values.');
if(!!document.abc && document.abc !=null) {
abcValue = document.abc;
} if(!!element.abc && typeof document.abc == "undefined") {
abcValue = element.abc;
}
if(!!document.xyz && document.xyz != null) {
xyzValue = document.xyz;
}if(!!element.xyz && typeof document.xyz == "undefined") {
xyzValue = element.xyz;
}
tempJson = {"id": baseID, "abc": abcValue, "xyz": xyzValue};
finalOutput.push(tempJson);
}
});
}
}
context.bindings.StatusOutput = finalOutput;
}
context.done();
}
Whenever i run the trigger function it is throwing the below error since the data for the unique key is already there in the collection.
Entity with the specified id already exists in the system
Is there any way to resolve this issue and update the cosmosDB collection if the unique key is already there in collection.
I am creating the DB and Trigger functions from Azure portal only. I have searched for a solution here but nowhere i saw a solution for which triggering function created through azure portal.