且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Azure函数插入但不更新cosmosDB

更新时间:2023-02-14 09:11:17

该绑定确实会执行Upsert操作.

The binding does indeed do an Upsert operation.

我创建了此示例函数,该函数采用Http有效负载(JSON),并将其按原样存储在Cosmos DB中:

I created this sample Function that takes an Http payload (JSON) and stores it in Cosmos DB as-is:

[FunctionName("Function1")]
public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req,
   [DocumentDB("MyDb", "MyCollection", ConnectionStringSetting = "MyCosmosConnectionString")] out dynamic document,
   TraceWriter log)
{
    log.Info("C# HTTP trigger function processed a request.");

    dynamic data = req.Content.ReadAsAsync<object>().GetAwaiter().GetResult();
    document = data;

    return req.CreateResponse(HttpStatusCode.OK);
}

如果我将JSON有效负载发送到Http端点,则输出绑定将按预期工作:

If I send a JSON payload to the Http endpoint, the output binding works as expected:

当我检查数据浏览器时,我看到:

When I check the Data Explorer, I see:

这次,如果我发送第二个有效负载,则添加一个属性(相同的ID):

If I send a second payload, this time, adding a property (same id):

数据浏览器显示文档已更新,并且具有相同的功能代码:

The Data Explorer shows the document was updated, with the same Function code:

您可以添加完整的异常/错误跟踪吗?您的服务总线消息中是否包含"id"?您的收藏是否已分区?

Can you add the full Exception/Error trace? Is your Service Bus Message including an "id"? Is your collection partitioned?

如果您的集合已分区并且您正在更改Partition键属性的值,则绑定将不会更新现有文档,它将创建一个新的文档,因为Upsert操作将找不到现有文档(基于在ID/分区键上).但这不会引发异常.

If your collection is partitioned and you are changing the value of the Partition key property, then the binding won't update the existing document, it will create a new one because the Upsert operation won't find an existing document (based on the id/partition key). But it won't throw an exception.