如何使用带有HTTP触发器POST的Azure功能更新Cosmos Azure(NOSQL)中的数据

时间:2017-07-06 14:36:22

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

我是一个新手Azure,我正在尝试将Azure功能用于HTTP触发器NodeJS, 我已经在Azure Cosmos上有一个NoSQL数据库。 例如:

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

然后我想将一个名为 apple 的新水果添加到水果数组中,或者从衣服中删除婴儿 >。同时将男性更新为男性, 我能怎么做 ? 我找到了 context.bindings 。但我还不知道如何使用它, 有人可以帮助我吗?

非常感谢。

2 个答案:

答案 0 :(得分: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();
};

答案 1 :(得分:0)

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
}

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