Cosmos DB "Partial Update"/Patch, cant set new property value to null

Viewed 1092

I'm trying out the Cosmos DB SDK's new Patch/Partial Update-functionality (for .NET)

When adding a new property I use

 var patchOperations = new List<PatchOperation>(){
       PatchOperation.Add<string>("/FavoriteColor", **null**)
 };


 await container.PatchItemAsync<T>(
                          id: myId,
                          partitionKey: new PartitionKey(myPk),
                          patchOperations: patchOperations);

The problem is, that it throws at the PatchOperation-Add() if I set second parameter to null (with message "Value cannot be null"). I can set any non-null string and it works well. I just wonder if this isn't supported yet or if I missed something.

3 Answers

Thanks to github user rvdvelden (source), this work around appears to work perfectly:

private JToken NullValue { get { return new JProperty("nullValue", null).Value; } }

Used in this way:

operations.Add(PatchOperation.Set("\customer", value ?? NullValue));

Remove is one alternative if the intent is to remove field/property.

This is not supported with Patch yet,

However, if you want to remove the entire property you need to use Remove Operation

Related