JViM
JViM

Reputation: 1

How can i update data in Cosmos Azure (NOSQL) , using Azure function with HTTP trigger POST

I'm a newbie Azure, and i'm trying to use Azure function for HTTP trigger NodeJS, I already have an NoSQL database on Azure Cosmos. Example:

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

Then i want to ADD a new fruit named apple into fruits array, or REMOVE babies out of clothes. Also UPDATE man to men, How can i do ? I found the context.bindings. But i haven't known how to use it yet, Does anyone can help me?

Many thanks.

Upvotes: 0

Views: 616

Answers (2)

Janusz Nowak
Janusz Nowak

Reputation: 2848

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

and 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
}

Use coresponding name instead for task.

Upvotes: 0

Mikhail Shilkov
Mikhail Shilkov

Reputation: 35134

Here is a sample function which increments num field of the document.

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

Upvotes: 1

Related Questions