简体   繁体   English

Azure Cosmos DB部分更新和补丁操作替换路径

[英]Azure Cosmos DB Partial Update and Patch Operation Replace Path

I have a JSON schema with a structure that follows:我有一个 JSON 架构,其结构如下:

PARENT 1 to Many SET 1 to Many TASK.父母 1 到许多 SET 1 到许多任务。

I have the code below that takes an updated task (updatedTask) and using the PatchItemAsync function tries to update a specific document (as set by document id) within a partition (as with with partitionKey).我有下面的代码,它接受更新的任务 (updatedTask) 并使用 PatchItemAsync function 尝试更新分区内的特定文档(由文档 ID 设置)(与 partitionKey 一样)。 However since I am updating a grandchild item (ie task) in my JSON document I also am trying to drill down from parent to set (by passing the set id) and then updating the appropriate task (by setting task id).但是,由于我正在更新我的 JSON 文档中的孙项(即任务),我还尝试从父项向下钻取到集合(通过传递集合 ID),然后更新适当的任务(通过设置任务 ID)。 The result I'm getting a object not set error.结果我得到了 object not set 错误。

Most of the examples show drilling down using the index but I don't know where in the order SET and the TASK are but do have their IDs.大多数示例显示使用索引向下钻取,但我不知道 SET 和 TASK 的顺序在哪里,但确实有它们的 ID。

Any thoughts on how I should set the path so I can replace the current task with the new task value?关于如何设置路径以便用新任务值替换当前任务有什么想法吗? I appreciate your assistance and guidance.感谢您的帮助和指导。

    public async Task<string> UpdateIT(string docid, Task updatedTask, string path, string taskid, string setid)
    {
           try
        {
            PatchItemRequestOptions patchItemRequestOptions = new PatchItemRequestOptions
            {
                FilterPredicate = path
            };
            ItemResponse<IndividualTask> response = await container.PatchItemAsync<IndividualTask>
                (id: docID, partitionKey: new Microsoft.Azure.Cosmos.PartitionKey(taskid), patchOperations: new[] { PatchOperation.Replace("/PARENT/SET/[?(@.SETID=='" + setid + "')]/TASK/[?(@. TASKID=='" + taskid + "'")], updatedIT) }, patchItemRequestOptions);
            string status = response.StatusCode.ToString();
            return status;
        }
        catch (Exception e)
        {
            return e.Message;
        }
}

Also I have provided the JSON schema.我还提供了 JSON 架构。 I'm trying to update all of the properties in the Task (ie TaskID, TaskStatus, TaskTitle, etc.).我正在尝试更新任务中的所有属性(即 TaskID、TaskStatus、TaskTitle 等)。

{
"PARENT": [
    {
        "_type": null,
        "PARENTID": "GUID",
        "PARENTTitle": null,
        "PARENTDescription": null,
        "SET": [
            {
                "SETID": "GUID",
                "CreatedBy": "",
                "CreatedDate": "0001-01-01T00:00:00",
                "TASK": [
                    {
                        "TASKID": "GUID",
                        "TaskStatus": "",
                        "TaskTitle": "",
                        "TaskType": "",
                        "TaskDescription": "",
                        "TaskNotes": "",
                        "Priority": "Normal",
                        "CreatedBy": "",
                        "CreatedDate": "0001-01-01T00:00:00"
                    }
                ]
            }
        ]
    }
],
"CaseID": "GUID",
"TenantID": "testtenant0004",
"id": "GUID",
"TaskID": "GUID",

You can just do like this, just set the path of the item you need to update你可以这样做,只需设置你需要更新的项目的路径

Task test = new();
test.Description = "test description 01";
test.Id = "00123";    
ItemResponse<SalesOrder> response = await 
container.PatchItemAsync<SalesOrder>(
    id: docID,
    partitionKey: new PartitionKey("partitionkey-value"),
    patchOperations: new[] { PatchOperation.Set("/PARENT/0/SET/0/TASK/0", test) });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM