Azure Cosmos DB Partial Update and Patch Operation Replace Path

Viewed 2290

I have a JSON schema with a structure that follows:

PARENT 1 to Many SET 1 to Many TASK.

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). However since I am updating a grandchild item (i.e. 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). The result I'm getting a object not set error.

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.

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. I'm trying to update all of the properties in the Task (i.e. TaskID, TaskStatus, TaskTitle, etc.).

{
"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",
1 Answers

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) });
Related