Anish
Anish

Reputation: 233

CosmosDB SQL API: Change property Name - (Partial Updates .Net SDK or loops)

Consider the following data Data

I am trying to update the property name from givenName to PetName. Below are the things I tried.

Option 1:

List<PatchOperation> patchOperations = new List<PatchOperation>();

dynamic data = new
{
    PetName= "Dummy"
};

//Remove the 0th element from the pet array 
patchOperations.Add(PatchOperation.Remove("/children/0/pets/0"));
//insert the new entity
patchOperations.Add(PatchOperation.Add<dynamic>("/children/0/pets/0", data));

await Container.PatchItemAsync<dynamic>("AndersenFamily", new 
PartitionKey("AndersenFamily"), patchOperations);

This works on the first element (as I have specified the index as 0). enter image description here

But I couldn't find a way to update all the elements in the array. Something like /children/*/pets (I get an error "Invalid navigation for array(*)").

Option 2:

Read all the data from the database, remove the existing pets and upload the new object with the property PetName. But I guess it will take lot of time to loop through all the objects and make these changes.

Can someone suggest a better solution or a way to fix the partial updates so that it updates all the elements in the array?

The actual dataset that I am working on is huge and has more nested arrays.

Upvotes: 0

Views: 722

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222582

Yes you are right, Current version of Patch does not support patching nested array. You need to know the index in order to replace/add values as you mentioned in the first question.

One way to think about this would be to write your own logic to replace the keys , there are libraries available in dotnet and then using the bulk insert using the SDK to insert those documents.

Upvotes: 1

Related Questions