使用JavaScript存储过程替换和更新ID

使用JavaScript存储过程替换和更新ID,javascript,json,azure,azure-cosmosdb,Javascript,Json,Azure,Azure Cosmosdb,背景: 我需要用JavaScript(在CosmosDB中)创建一个存储过程,其中:对于每个反馈文档,用新id替换/更新Feedback.id var NewID = "5678" { "Feedbacks" : [ { "id": "1234" } { "id": "1234" } ] } 这就是我正在做的: 我创建了一个名为Update

背景:

我需要用JavaScript(在CosmosDB中)创建一个存储过程,其中:对于每个反馈文档,用新id替换/更新Feedback.id

var NewID = "5678"

{

"Feedbacks" : [
 {
    "id": "1234"
 }
 {
    "id": "1234"
 }  
] 

}
这就是我正在做的:

我创建了一个名为UpdateID的函数,并将参数设置为OldID和NewID。我是说遍历文档,对于每个旧ID值,替换为新ID。我对Python比较熟悉,所以这对我来说有点不同,我不确定这是正确的方法

For every iteration in doc.Feedbacks:

function UpdateID(OldID, NewID) {

     if (Feedbacks.id = "OldID") 
     
     

}

任何建议都会很有帮助

编写javascript的方法有很多,但有一种方法应该走上正确的轨道:

function UpdateID(oldID, newID) {
    //get just the records that need updating
    var isAccepted = __.filter(
        function(doc) { 
            return doc.Feedbacks && doc.Feedbacks.findIndex(function(feedback){
                return feedback.id == oldID
            }) > -1;
        },
        function (err, feed, options) {
            if (err) throw err;

            // Check the feed and if empty, set the body to 'no docs found'
            if (!feed || !feed.length) {
                var response = getContext().getResponse();
                response.setBody('no docs found');
            }
            else {
                //update the documents that have the old id
                UpdateDoc(oldID, newID, feed)
            }
    });

    if (!isAccepted) throw new Error('The query was not accepted by the server.');
}

//function based on https://stackoverflow.com/questions/36009939/documentdb-updating-multiple-documents-fails
function UpdateDoc(oldID, newID, documents) {
    console.log("updating " + documents.length + " docs")
    if (documents.length > 0) {
        var document = documents[0];

        // DocumentDB supports optimistic concurrency control via HTTP ETag.
        var requestOptions = { etag: document._etag};

        document.Feedbacks =  document.Feedbacks.map(function(feedback){
            if(feedback.id === oldID) {
                feedback.id = newID;
            }

            return feedback;
        });

        // Update the document.
        var isAccepted = __.replaceDocument(document._self, document, requestOptions, function(err, updatedDocument, responseOptions) {
           if (err) {
              responseBody.error = err;
              throw err;
           }
        });

        // If we hit execution bounds - throw an exception.
        if (!isAccepted) {
            responseBody.log += "Update not accepted";
            response.setBody(responseBody);
        }
        else {
            documents.shift();
            if(documents.length > 0){
                UpdateDoc(oldID, newID, documents);
            }
        }
    }
}

您的新id存储在哪里以供反馈?它位于LogicApp的输出中。它存储在LogicApp创建的一个变量中看起来这里有很多信息需要保密,您的团队中是否有人可以提供帮助?不幸的是,这个问题太模糊了,无法得到任何合理的答案。让我重新表述一下。谢谢