通过 azure 函数删除 CosmosDB 中的文档 [英] Delete document in CosmosDB through azure function

查看:22
本文介绍了通过 azure 函数删除 CosmosDB 中的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读 Azure 门户后,我了解了如何通过 Azure Functions 对 CosmosDB 进行 POSTPUTGET 操作.但是删除,我不明白该怎么做.

Reading the Azure portal I've understood how to make a POST, PUT and GET operation with CosmosDB through the Azure Functions. But deleting, I don't understand how to do this.

我应该使用哪些绑定.它应该通过 sql 查询还是集合的方法发生,例如 Remove()?

Which bindings should I use. Should it occur either through sql query or collection's method, like Remove()?

        [**FunctionName**("EmployeeDocumentDB")]
        public static async Task<HttpResponseMessage> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post", "put", "delete", Route = "EmployeeDocumentDB/partitionkey/{key}/id/{id}")]HttpRequestMessage req,
        [DocumentDB(
        databaseName: "MyDatabase",
        collectionName: "MyCollection",
        ConnectionStringSetting = "CosmosDBEmulator")] ICollector<Person> outputDocument,
        TraceWriter log)
    {
        dynamic data = await req.Content.ReadAsAsync<Person>();

        return req.CreateResponse(HttpStatusCode.Accepted);
    }

推荐答案

我合并了:

  • HTTP 触发器
  • CosmoDB DocumentClient 输入
  • 从查询字符串中查找 ID 的 CosmoDB 输入
public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "delete")] HttpRequest req,
            [CosmosDB(databaseName: "storage", collectionName: "pizza", Id = "{Query.id}", PartitionKey = "{Query.storeId}", ConnectionStringSetting = "..."] Document document,
            [CosmosDB(databaseName: "storage", collectionName: "pizza", ConnectionStringSetting = ...)] DocumentClient client)
        {
            string storeId = req.Query["storeId"];

            if(document == null || string.IsNullOrEmpty(storeId))
                return new BadRequestResult();

            await client.DeleteDocumentAsync(document.SelfLink, new RequestOptions() { PartitionKey = new PartitionKey(storeId) });

            return new OkResult();
        }

这篇关于通过 azure 函数删除 CosmosDB 中的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆