使用Azure函数删除CosmosDB条目 [英] Delete CosmosDB entry using Azure Function

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

问题描述

我一直在寻找一种技术,通过浏览器内的代码编辑器使用Azure函数删除Cosmos数据库中的项。我不想在VS上使用本地开发的代码的原因有很多。

我使用的代码是可用的here,我使用的是带有CosmosDB输入和输出绑定的HttpTrigger。它们的命名相当明显(inputDocument、outputDocument)。

这段代码在从数据库中读取项目和写入新文档方面的工作非常出色,但是我希望能够删除单个项目。我正在制作一个游戏"拍卖行"系统,要"购买"一件物品,我需要将它从数据库中删除。

我现在已经搜索了相当多的地方,很多人说要使用DocumentDB,但我认为浏览器编辑器不能支持这一点,我不能让它识别正确的Azure库来使用它。如果我走错了一步,请告诉我。添加时失败

#r "Microsoft.Azure.Documents.Client"
using Microsoft.Azure.Documents.Client;

编辑 在与Azure支持人员交谈后,我发现v3使用的是Documents.Core,而不是Documents.Client。如果有人能为Document.Core提供文档,我将不胜感激!

谢谢。下面复制的代码;

    #r "Newtonsoft.Json"

    using System.Net;
    using System.Linq;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Primitives;
    using Newtonsoft.Json;

    public class AuctionItem
    {
        [JsonProperty("itemID")]
        public string itemID { get; set; }
    
        [JsonProperty("price")]
        public string Price { get; set; }
    
        [JsonProperty("amount")]
        public string Amount { get; set; }
    }

    public static IActionResult Run(HttpRequest req, out object outputDocument, 
    IEnumerable<AuctionItem> inputDocument, ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        string name = req.Query["name"];
        string price = req.Query["price"];
        string amount = req.Query["amount"];

        string command = req.Query["command"];

        outputDocument = null;

        if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(price) && !string.IsNullOrEmpty(amount)) 
        {
            string responseMessage = "{"Message":"Success",
"Data": [" + "
";
               
            if (!string.IsNullOrEmpty(command)) {
                if (command == "1") {
                    foreach (var item in inputDocument) {
                        responseMessage += "{
	"itemID":"" + item.itemID + ""," + "
";
                        responseMessage += "	"price":"" + item.Price + ""," + "
";
                        responseMessage += "	"amount":"" + item.Amount + ""}," + "
";
                    }
                } else if (command == "2") {
                    var item = inputDocument.Where(x => x.itemID == name).FirstOrDefault();
                    if (item != null) {
                        inputDocument = inputDocument.Where(x => x != item);
                    }
                } else if (command == "3") {
                    responseMessage += "{
	"itemID":"" + name + "",
";
                    responseMessage += "	"price":"" + price + "",
";
                    responseMessage += "	"amount":"" + amount + ""}
";
                

                    log.LogInformation(responseMessage);
                    outputDocument = new {
                        itemID = name,
                        price = price,
                        amount = amount
                    };
                }
            }
        
            responseMessage += "]}";
            return new OkObjectResult(responseMessage);
        } else {
            outputDocument = null;
            return new BadRequestResult();
        }
            
    }

推荐答案

您需要在Http触发器中创建名为";function.proj";的文件。

内容:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netcoreapp3.0</TargetFramework>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.12.0" />
    </ItemGroup>
</Project>

创建步骤:

1.切换到经典体验

2.创建文件

下面是我的测试代码,运行正常:

#r "Newtonsoft.Json"
#r "Microsoft.Azure.DocumentDB.Core"

using System.Net;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using System;
using System.Threading.Tasks;

private static DocumentClient client;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    client = new DocumentClient(new Uri("endpointUrl"), "authorizationKey");
    ResourceResponse<Document> response = await client.DeleteDocumentAsync(
        UriFactory.CreateDocumentUri("databaseName", "collectionName", "id"),
        new RequestOptions { PartitionKey = new PartitionKey("partition-key-value") });

    log.LogInformation("Request charge of delete operation: {0}", response.RequestCharge);
    log.LogInformation("StatusCode of operation: {0}", response.StatusCode);


    return new OkResult();    
}

顺便说一下,您可以找到文档here

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

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