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

查看:67
本文介绍了通过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);
    }

推荐答案

您可以通过直接绑定到DocumentClient本身并以编程方式删除文档来实现此目的.

You could do this by binding directly to the DocumentClient itself, and delete the Document programatically.

[FunctionName("DeleteDocument")]
public static async Task Run(
    [TimerTrigger("00:01", RunOnStartup = true)] TimerInfo timer,
    [DocumentDB] DocumentClient client,
    TraceWriter log)
{
    var collectionUri = UriFactory.CreateDocumentCollectionUri("ItemDb", "ItemCollection");
    var documents = client.CreateDocumentQuery(collectionUri);

    foreach (Document d in documents)
    {
        await client.DeleteDocumentAsync(d.SelfLink);
    }
}

请参见 CosmosDBSamples

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

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