Azure Cosmos Partial Update

Deepak Patil 61 Reputation points
2022-01-24T19:20:46.307+00:00

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

I want to add new property domainTag.mm and before doing that i need to check wheather domainTag.mm 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.

Schema:
{
"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": [
"value3",
"valu4"
]
},
"_ts": 1641295580
}

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

Accepted answer
  1. Anurag Sharma 17,591 Reputation points
    2022-01-25T06:45:25.207+00:00

    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