【问题标题】:update and delete documents in cosmos db through azure functions通过 azure 函数更新和删除 cosmos db 中的文档
【发布时间】:2018-06-09 23:29:36
【问题描述】:

我是 cosmos db 以及 azure 函数的新手,但我的速度很快。我已经能够在阳光下找到每个教程来创建和阅读文档,但不能更新和删除。似乎没有人拥有使用 azure 函数的完整 CRUD 教程。

有人可以向我展示一个典型的 .csx 文件,它采用 azure 函数,它接收一个文档、更新它并返回一个 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
}

【问题讨论】:

    标签: azure azure-cosmosdb azure-functions csx


    【解决方案1】:

    更新文档的函数的最简单示例看起来与创建文档的函数完全相同:函数将根据指定 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
    }
    

    【讨论】:

    • 添加到 Mikhail 的回复中,您可以在 this article 中找到示例
    • 我已经在这篇文章上下翻阅了答案。我认为米哈伊尔的回答回答了这个问题。我一直在寻找通过csx更新的概念,他的代码很容易理解。
    • 你能分享一个函数来显示如何删除绑定中“id”定义的文档吗?
    • 我已经分配给了 out 变量,它创建了一个新文档,而不是更新它,有什么线索吗?
    猜你喜欢
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多