通过azure函数更新和删除cosmos db中的文档

时间:2017-12-29 14:22:59

标签: azure azure-cosmosdb azure-functions csx

我是宇宙数据库以及天蓝色功能的新手,而且我无法快速完成任务。我已经能够在阳光下找到每个教程来创建和阅读文档,但不能更新和删除。似乎没有人使用azure函数获得完整的CRUD教程。

有人可以在azure函数中向我显示一个典型的.csx文件,该文件包含文档,更新文档并返回OK响应吗?

我已经尝试过这个

#load "..\Shared\Classes.csx"
using System.Net;

public static HttpResponseMessage Run(HttpRequestMessage req, 
IEnumerable<Business> businessToBeUpdated, out dynamic updatedBusiness, 
TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");

//compiler requires this assignment
updatedBusiness = null;

// Get request body
Business data = req.Content.ReadAsAsync<Business>().Result;
businessToBeUpdated = businessToBeUpdated.FirstOrDefault<Business>();
log.Info(businessToBeUpdated.Count().ToString());
if(businessToBeUpdated != null && data != null)
{
    //update it
    businessToBeUpdated = data;
    //updatedBusiness.id = data.id;
    log.Info(businessToBeUpdated.website);
}
else{
    return req.CreateResponse(HttpStatusCode.BadRequest);
}

return req.CreateResponse(HttpStatusCode.OK);
}

这是与之相关的绑定。

{
"bindings": [
{
  "authLevel": "anonymous",
  "name": "req",
  "type": "httpTrigger",
  "direction": "in",
  "route": "updatebiz/{id}"
},
{
  "name": "$return",
  "type": "http",
  "direction": "out"
},
{
  "type": "documentDB",
  "name": "businessToBeUpdated",
  "databaseName": "dbname",
  "collectionName": "Businesses",
  "sqlQuery": "Select * FROM c where c.id = {id}",
  "connection": "connection",
  "direction": "in"
},
{
  "type": "documentDB",
  "name": "updatedBusiness",
  "databaseName": "dbname",
  "collectionName": "Businesses",
  "createIfNotExists": false,
  "connection": "connection",
  "direction": "out"
  }
],
"disabled": false
}

1 个答案:

答案 0 :(得分:4)

更新文档的函数的最简单示例与创建文档的函数完全相同:函数将根据指定的id的文档是否已存在来执行其中一个。

您没有提到您面临的确切问题。您的代码甚至没有编译,具有相互分配的可枚举和单个对象。除此之外,您永远不会将updatedBusiness分配给null以外的任何内容。

我带来了一个实际的例子,它做了我认为你想要完成的事情。

csx脚本:

using System.Net;

public class Business
{
    public string id { get; set;}
    public string name { get; set;}
}

public static HttpResponseMessage Run(HttpRequestMessage req, 
    Business businessToBeUpdated, out Business updatedBusiness, TraceWriter log)
{
    log.Info("C# HTTP trigger function processed a request.");

    var data = req.Content.ReadAsAsync<Business>().Result;
    if(businessToBeUpdated == null || data == null)
    {
        updatedBusiness = businessToBeUpdated;
        return req.CreateResponse(HttpStatusCode.BadRequest);
    }

    updatedBusiness = data; 
    // or merge data and businessToBeUpdated in some desired way

    return req.CreateResponse(HttpStatusCode.OK);
}

function.json

{
  "bindings": [
    {
      "authLevel": "anonymous",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "route": "updatebiz/{id}"
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "type": "documentDB",
      "name": "businessToBeUpdated",
      "databaseName": "dbname",
      "collectionName": "Businesses",
      "id": "{id}",
      "connection": "connection",
      "direction": "in"
    },
    {
      "type": "documentDB",
      "name": "updatedBusiness",
      "databaseName": "dbname",
      "collectionName": "Businesses",
      "id": "{id}",
      "connection": "connection",
      "direction": "out"
    }
  ],
  "disabled": false
}