2
votes

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.

1
can you post the codeSajeetharan
you are missing the main part, which is the logic implementationSajeetharan
When you are using Unique Keys, the check is related to having 2 different documents with the same Unique Key value. The Cosmos DB output binding will try to do an Upsert operation, so if the document already exists, it will try to update it, if not, create it. This error should appear if you are trying to insert a document with different id, but same Unique Key than another existing document. Did you double check that you are sending the same id as the document that already has that Unique Key value?Matias Quaranta
thanks you @Matias Quaranta I missed to get id from existing record and update the same with new data to be upserted to cosmos Collection. Its working fine now.Antony
@Matias Quaranta : So whatever unique Key is set on a collection, the upsert process will happen based on id existing in the collection ?Antony

1 Answers

1
votes

Whenever you want to update the record if already existing in cosmosDB from Azure trigger function, below points should be taken care.

  1. Check for the element named id existing in DB collection for a Unique key
  2. The id should be same for the data which is updating for a Unique key while upserting.

If the element id is not same then, while upserting cosmos will assign a new value for that element and we will have 2 entry for a Unique key with different ids and throw the above error.