阅读 Azure 门户后,我了解了如何通过 Azure Functions 使用 CosmosDB 进行 POST
、PUT
和 GET
操作。但是删除,我不明白该怎么做。
我应该使用哪些绑定(bind)。它应该通过 sql 查询还是集合的方法(例如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 - 通过azure函数删除CosmosDB中的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46505301/