Partial Update was one of the most wanted features by Cosmos DB customers. In a regular update operation, you need to send the whole JSON document to Cosmos DB. This can be silly if your data model is large and you want to update one field in it. With a regular update, your request object will be large because you need to send the whole data model. Regular Update operation needs more resources from the client/SDK and network bandwidth.
You might think that partial updates might cost fewer request units. Unfortunately, this is not the case. Because Cosmos DB still needs to open the JSON document, change the necessary properties and save the data. Cosmos DB uses almost the same amount of CPU and memory for this operation for a regular update or a partial update.
Following operations are supported with Partial Update
Add
If the target does not exist, It will be added.
If the target exists, Its value will be replaced.
If the target is an array, It will be added to the beginning of the array.
To append to an array, you need to pass the length of the array as an index or you can use - character.
If you specify an index greater than the array length, you will receive an error.
Set
This operation same as Add. Only difference is with the Array data type. You can update an array item with this operation by giving a valid array index.
Replace
This operation updates current property values or array items. If the given array index does not exist, you will receive an error.
Remove
If the specified path does not exist, you will receive an error.
If the given path exists, the property will be removed.
If the target path has an array index, the array item will be removed.
Increment
This operation increments the specified field. You can pass a negative number to decrease values.
If the given field does not exist, It will create the field and set it to the specified value.
In the following example, I am replacing Title property by using Partial Update. As you can see, you must pass id and partition key of the document for partial update to work.
Container container = cosmosClient.GetContainer("Stackoverflow", "Posts");
ItemResponse<StackOverflowPost> response = await container.PatchItemAsync<StackOverflowPost>(
id: "da98d94d-c9ed-49d0-b338-17cb4d909bb5",
partitionKey: new PartitionKey(99999),
patchOperations: new[]{
PatchOperation.Replace("/Title","Updating by PartialUpdate()")
});
In the following example, I am passing multiple operations with one request
Container container = cosmosClient.GetContainer("Stackoverflow", "Posts");
var ops = new List<PatchOperation>(){
PatchOperation.Replace("/Title","Updated by PartialUpdates()"),
PatchOperation.Increment("/Score",1)
};
ItemResponse<StackOverflowPost> response = await container.PatchItemAsync<StackOverflowPost>(
id: "da98d94d-c9ed-49d0-b338-17cb4d909bb5",
partitionKey: new PartitionKey(99999),
patchOperations: ops
);
In the following example, I am using the filter function of partial update. Partial Update will run if Score's value is less than 100
Container container = cosmosClient.GetContainer("Stackoverflow", "Posts");
PatchItemRequestOptions options = new PatchItemRequestOptions()
{
FilterPredicate = "FROM p WHERE p.Score < 100"
};
var ops = new List<PatchOperation>(){
PatchOperation.Increment("/Score",10)
};
ItemResponse<StackOverflowPost> response = await container.PatchItemAsync<StackOverflowPost>(
id: "da98d94d-c9ed-49d0-b338-17cb4d909bb5",
partitionKey: new PartitionKey(99999),
patchOperations: ops,
requestOptions: options
);
Hello
ReplyDeleteMy name is Elisabeth and I would like to ask if there is any Guest or Sponsored post option available on your website
I would like to post a unique and high quality article with a dofollow link inside
Please let me know all the guidelines for a perfect article
Best Regards
Elisabeth Muller
elismullermarketing@gmail.com