Azure Cosmos Partial Update

Deepak Patil 61 Reputation points

I am trying to do partial update operation using Azure Comos Java SDK.

I want to add new property and before doing that i need to check wheather is already present or not.

how to do this using azure java sdk.
if i do directly cosmosPatchOperations.add("/domainTag/mm/test",1); it gives error if mm is not present domainTag is not present.

"countryDcOrgUnit": "us-06085-1008",
"locationName": "TESTSLOT28",
"depth": 34,
"locationType": "Door",
"attributes": {},
"id": "TESTSLOT28",
"_rid": "mlIsAMtatCgBAAAAAAAAAA==",
"_self": "dbs/mlIsAA==/colls/mlIsAMtatCg=/docs/mlIsAMtatCgBAAAAAAAAAA==/",
"_etag": "\"a50162b9-0000-0500-0000-61d42edc0000\"",
"_attachments": "attachments/",
"domainTag": {
"mm": {
"haul": "Y",
"put-away": "N"
"loading": {
"inbound": "Y"
"domain1": {
"tag3": "Y",
"tag1": "Y"
"properties": {
"key1": "value1",
"key2": "value2",
"key3": [
"_ts": 1641295580

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,432 questions
{count} votes

Accepted answer
  1. Anurag Sharma 17,566 Reputation points

    Thanks for replying back. One way I know of is to read the documents. The first step in below code will retrieve the document and then we can write custom code to check if any attribute exists or not. Then we can add them as per step 2 and step 3.

    //step 1 
    UserInfo user = container.readItem(user.getId(), new PartitionKey(user.getEmail()), UserInfo.class); 
    //step 2 
    CosmosPatchOperations patchOps = CosmosPatchOperations.create().add("/phone/2",12345).set("/address","123 Foobar"); 
    //step 3 
    container.patchItem(user.getId(), new PartitionKey(user.getEmail()), patchOps, reqOps, UserInfo.class);

    Referenced Article: Partial Document Update in Azure Cosmos DB

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful