无法从 Azure 触发函数更新 cosmosDB 集合

Unable to update the cosmosDB collection from Azure triggering function

我在 cosmosDB 中创建了一个带有唯一键的集合。理想情况下,如果输入现有键作为集合的输入,则应使用新值更新集合。 我有一个 Azure cosmosDB 触发器函数,它将上述集合配置为输出。

下面是我正在执行逻辑实现的 index.js 文件。

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(); 
 }

每当我 运行 触发器函数时,它都会抛出以下错误,因为唯一键的数据已经存在于集合中。


Entity with the specified id already exists in the system

如果唯一键已经存在于集合中,是否有任何方法可以解决此问题并更新 cosmosDB 集合。

我仅从 Azure 门户创建数据库和触发器函数。我在这里搜索了一个解决方案,但在任何地方都找不到通过 azure 门户创建触发功能的解决方案。

如果要通过 Azure 触发器功能更新已存在于 cosmosDB 中的记录,则应注意以下几点。

  1. 检查数据库集合中存在的名为 id 的元素的唯一键
  2. id 应该与在更新时更新唯一键的数据相同。

如果元素 id 不相同,那么在更新 cosmos 时将为该元素分配一个新值,我们将有 2 个具有不同 [=14 的唯一键的条目=]ids 并抛出上述错误。