【问题标题】:How do I delete a document from CosmosDB using an Azure Cloud Function?如何使用 Azure 云函数从 CosmosDB 中删除文档?
【发布时间】:2018-10-09 08:56:52
【问题描述】:

我知道如何使用 Azure Cloud Functions 在我的 CosmosDB 实例中创建、读取和更新文档,但仍然不知道如何实现删除。我用作参考的主要文档是https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-cosmos-db-triggered-function

我尝试使用不带 id 的输入和输出绑定(从而获取整个集合),然后过滤我要从 inputDocuments 中删除的项目并将结果设置为 outputDocuments。不幸的是,这似乎并没有真正从数据库实例中删除该项目。知道我可能会错过什么吗?这样做的正确方法是什么?

这是我写的代码:

const { id } = req.query;
context.bindings.outputDocuments = context.bindings.inputDocuments;
const index = context.bindings.outputDocuments.findIndex(doc => doc.id === id);
const doc = context.bindings.outputDocuments.splice(index, 1);

我也尝试了一个更简单的版本,但这并没有什么不同:

const { id } = req.query;
context.bindings.outputDocuments = context.bindings.inputDocuments.filter(doc => doc.id !== id);

我使用日志语句验证了我的函数正确地更新了上述两种实现的 outputDocuments。

这是我的绑定:

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "delete"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "documentDB",
      "name": "inputDocuments",
      "databaseName": "heroesDatabase",
      "collectionName": "HeroesCollection",
      "connection": "ydogandjiev-documentdb_DOCUMENTDB",
      "direction": "in"
    },
    {
      "type": "documentDB",
      "name": "outputDocuments",
      "databaseName": "heroesDatabase",
      "collectionName": "HeroesCollection",
      "createIfNotExists": false,
      "connection": "ydogandjiev-documentdb_DOCUMENTDB",
      "direction": "out"
    }
  ],
  "disabled": false
}

【问题讨论】:

标签: azure azure-functions azure-cosmosdb


【解决方案1】:

您可以使用客户端并拨打DeleteDocumentAsync,如下例所述

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, string customerid, TraceWriter log, DocumentClient client)
{
    HttpResponseMessage response = new HttpResponseMessage();
    try {
        await client.DeleteDocumentAsync(UriFactory.CreateDocumentUri("BankDatabase","Customers", customerid));
        response.StatusCode = HttpStatusCode.OK;
        log.Info($"Deleted customer with id: {customerid}");
    }
    catch (DocumentClientException exc)
    {
        if (exc.StatusCode == HttpStatusCode.NotFound)
        {
            response = req.CreateResponse(HttpStatusCode.NotFound, "There is no item with given ID in our database!");
        }
        else
        {
            response = req.CreateResponse(HttpStatusCode.InternalServerError, "Internal server error. Contact administrator.");
        }
    }

    return response;
}

请在 Azure CosmosDB with Azure functions

上找到完整的 Repo

【讨论】:

  • 谢谢,您有一个如何在 JavaScript 函数中使用客户端的示例(这是我一直在尝试使用的)吗?
  • @YuriDogandjiev 检查这个样本github.com/Azure/azure-documentdb-node-q/blob/…
  • 我不太确定我是否遵循您的建议。在 C# 函数中,客户端按字面意思传入并准备就绪。在您向我指出的示例中,客户端必须初始化并连接到数据库。这就是你建议我在每次执行 Azure 函数时做的事情吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-23
  • 1970-01-01
  • 2020-10-25
  • 1970-01-01
  • 2019-01-23
  • 1970-01-01
相关资源
最近更新 更多