【问题标题】:How can i update data in Cosmos Azure (NOSQL) , using Azure function with HTTP trigger POST如何使用带有 HTTP 触发器 POST 的 Azure 函数更新 Cosmos Azure (NOSQL) 中的数据
【发布时间】:2017-07-06 14:36:21
【问题描述】:

我是 Azure 新手,我正在尝试使用 Azure 函数来触发 HTTP 触发 NodeJS, 我已经在 Azure Cosmos 上有了一个 NoSQL 数据库。 示例:

{
   ...
   "shop":{
      "fruits":[
         "orange",
         "strawberry",
         "lemon"
      ],
      "clothes":[
         "man",
         "woman",
         "babies"
      ]
   }
   ...
}

然后我想在 fruits 数组中添加一个名为 apple 的新水果,或者从 衣服 中移除 babies >。还将 ma​​n 更新为 men, 我能怎么做 ? 我找到了 context.bindings。但是我还不知道怎么用 有谁能帮帮我吗?

非常感谢。

【问题讨论】:

    标签: javascript node.js azure azure-cosmosdb azure-functions


    【解决方案1】:

    这是一个增加文档的num 字段的示例函数。

    function.json

    {
      "bindings": [
        {
          "authLevel": "function",
          "type": "httpTrigger",
          "direction": "in",
          "name": "req",
          "route": "HttpTriggerJSUpdateDocument/{docid}"
        },
        {
          "type": "http",
          "direction": "out",
          "name": "res"
        },
        {
          "type": "documentDB",
          "name": "inputDocument",
          "databaseName": "MyDB",
          "collectionName": "MyCollection",
          "id": "{docid}",
          "connection": "mydocdb_DOCUMENTDB",
          "direction": "in"
        },
        {
          "type": "documentDB",
          "name": "outputDocument",
          "databaseName": "MyDB",
          "collectionName": "MyCollection",
          "createIfNotExists": false,
          "connection": "mydocdb_DOCUMENTDB",
          "direction": "out"
        }
      ],
      "disabled": false
    }
    

    index.js:

    module.exports = function (context, req) {
        let inputDocument = context.bindings.inputDocument;
        context.log('JavaScript HTTP trigger, current value: ' + 
            (inputDocument && inputDocument.num));
    
        inputDocument.num = inputDocument.num + 1;
    
        context.bindings.outputDocument = inputDocument;
    
        context.res = {
            body: 'Result is ' + inputDocument.num
        };
        context.done();
    };
    

    【讨论】:

    • 谢谢! @Mikkail
    【解决方案2】:
    using System.Net;
    
    public static HttpResponseMessage Run(HttpRequestMessage req, out object taskDocument, TraceWriter log)
    {
        string name = req.GetQueryNameValuePairs()
            .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
            .Value;
    
        string task = req.GetQueryNameValuePairs()
            .FirstOrDefault(q => string.Compare(q.Key, "task", true) == 0)
            .Value;
    
        string duedate = req.GetQueryNameValuePairs()
            .FirstOrDefault(q => string.Compare(q.Key, "duedate", true) == 0)
            .Value;
    
        taskDocument = new {
            name = name,
            duedate = duedate.ToString(),
            task = task
        };
    
        if (name != "" && task != "") {
            return req.CreateResponse(HttpStatusCode.OK);
        }
        else {
            return req.CreateResponse(HttpStatusCode.BadRequest);
        }
    }
    

    和function.json

    {
      "bindings": [
        {
          "authLevel": "function",
          "name": "req",
          "type": "httpTrigger",
          "direction": "in"
        },
        {
          "name": "$return",
          "type": "http",
          "direction": "out"
        },
        {
          "type": "documentDB",
          "name": "taskDocument",
          "databaseName": "taskDatabase",
          "collectionName": "TaskCollection",
          "createIfNotExists": true,
          "connection": "DOCUMENTDB",
          "direction": "out"
        }
      ],
      "disabled": false
    }
    

    使用对应的名称代替任务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      • 2020-05-14
      相关资源
      最近更新 更多