【问题标题】:Replace and update IDs using JavaScript stored procedure使用 JavaScript 存储过程替换和更新 ID
【发布时间】:2020-12-08 04:18:52
【问题描述】:

背景:

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

var NewID = "5678"

{

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

}

这就是我正在做的事情:

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

For every iteration in doc.Feedbacks:

function UpdateID(OldID, NewID) {

     if (Feedbacks.id = "OldID") 
     
     

}

任何建议都会有所帮助

【问题讨论】:

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

标签: javascript json azure azure-cosmosdb


【解决方案1】:

编写 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);
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 2014-06-23
    • 2012-03-15
    • 2013-11-27
    相关资源
    最近更新 更多