#Targeted patch for nested component

3 messages · Page 1 of 1 (latest)

sour mango
#

is it possible to update only one value in a nested object in a mutation?

long lightBOT
#

Hi @sour mango. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Yes, you can update a specific value in a nested object using Convex's patch method. When you use patch, you can specify just the fields you want to update, including nested paths.

Here's how you can update a specific value in a nested object:

// Update a specific field in a nested object
await ctx.db.patch(documentId, {
  "nestedObject.specificField": newValue
});

This approach is commonly used in migrations and mutations when you need to modify just one part of a document without affecting other fields. For example, when adding a default value to a field or changing the type of a field, you can use targeted patches to update only what's needed.

The patch method is particularly useful for these kinds of targeted updates because it allows you to modify specific parts of a document without having to read the entire document first, which helps avoid conflicts with other mutations that might be updating different fields of the same document.

If you're working with complex migrations, you might want to check out Convex's migration helpers which make it easier to perform targeted updates across many documents Migrating Data with Mutations.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: