
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeGood news - you can definitely do this in a cleaner way without fetching the entire document! Sanity's patch operations support targeting specific items within nested arrays using path conditions.
For your use case of inserting data into a nested subentries array, you can use the insert operation with a path that targets the specific parent entry by its _key. Here's how:
client
.patch('aFuiYGWUw0SSAvMnYQafry')
.insert('after', 'entries[_key=="E0vulMwINU3H9ETBqXnRT7"].subentries[-1]', [
{
_key: 'new-unique-key', // generate a unique key
data: 'your new data'
}
])
.commit()This approach:
entries array using [_key=="E0vulMwINU3H9ETBqXnRT7"]subentries array within that entry[-1] to reference the last item (or use a specific index like [0])If you want to update an existing item in the nested array instead of inserting, you can use set:
client
.patch('aFuiYGWUw0SSAvMnYQafry')
.set({
'entries[_key=="E0vulMwINU3H9ETBqXnRT7"].subentries[_key=="6b8c867d-36b9-4eff-97e0-9fd5be6c6ee4"].data': 'updated data'
})
.commit()The key is using JSONMatch-style path syntax with conditions like [_key=="value"] to target specific objects in arrays. This way you're only sending the minimal change needed, avoiding the fetch-edit-patch cycle and preventing data loss issues.
These operations are also "real-time safe," meaning they work correctly even when multiple users are editing simultaneously, which wouldn't be guaranteed with your fetch-then-replace approach. You can read more about patch operations in the docs.
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store