如何使用Azure云函数从CosmosDB中删除文档?
我知道如何使用Azure云函数在我的CosmosDB实例中创建、读取和更新文档,但仍然不知道如何实现删除。我用作参考的主要文档是 我尝试在没有id的情况下使用输入和输出绑定,从而获取整个集合,然后过滤要从inputDocuments中删除的项,并将结果设置为outputDocuments。不幸的是,这似乎并没有从数据库实例中实际删除该项。知道我遗漏了什么吗?正确的方法是什么 以下是我编写的代码:如何使用Azure云函数从CosmosDB中删除文档?,azure,azure-functions,azure-cosmosdb,Azure,Azure Functions,Azure Cosmosdb,我知道如何使用Azure云函数在我的CosmosDB实例中创建、读取和更新文档,但仍然不知道如何实现删除。我用作参考的主要文档是 我尝试在没有id的情况下使用输入和输出绑定,从而获取整个集合,然后过滤要从inputDocuments中删除的项,并将结果设置为outputDocuments。不幸的是,这似乎并没有从数据库实例中实际删除该项。知道我遗漏了什么吗?正确的方法是什么 以下是我编写的代码: const { id } = req.query; context.bindings.outputD
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
}
您可以使用客户端并调用以下示例中提到的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;
}
请在上找到完整的回购。您可以使用客户端并调用下面示例中提到的DeleteDocumentSync
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;
}
请在可能重复的上找到完整的Repo可能重复的感谢,你有一个如何在JavaScript函数中使用客户端的例子吗?这正是我一直尝试使用的吗?@YuriDogandjiev检查此示例我不太确定我是否遵循了你的建议。在C函数中,客户机被直接传入并准备就绪。在您向我指出的示例中,客户端必须初始化并连接到数据库。这就是你建议我在每次执行Azure函数时都要做的事情吗?谢谢,你有没有一个例子说明如何在JavaScript函数中使用客户端,这正是我一直在尝试使用的?@YuriDogandjiev检查这个示例我不太确定我是否遵循了你的建议。在C函数中,客户机被直接传入并准备就绪。在您向我指出的示例中,客户端必须初始化并连接到数据库。这就是你建议我在每次执行Azure函数时所做的吗?