StoicWanderer
StoicWanderer14mo ago

db.patch should leave existing fields unmodified

Hello Convex people, My problem is that I want to patch some document fields in my DB and it would be nice to don't make an extra query of my existing data in order to leave them as they are, so that I can use the db.patch function to add new fields to my documents. What doyou think, can that be feasible to make the db.patch to leave the existing fields intact?
4 Replies
lee
lee14mo ago
Leaving existing fields intact is the intended behavior of db.patch. Can you share your code or a repro of the behavior you're referring to?
StoicWanderer
StoicWandererOP14mo ago
It says in the docs that existing fields are overwritten, does that mean the existing fields are being cleared if i dont provide a value when i use db.patch? I have a feeling that i misunderstood the docs
lee
lee14mo ago
Ah i see. here's the paragraph from the docs, with some (wordy) detail injected to hopefully make the meaning more clear.
The db.patch method will patch an existing document, shallow merging it with the given partial document (i.e. the argument/input to db.patch). New fields [that exist in the argument but not the document] are added. Existing fields [that exist in both the argument and the document] are overwritten. Fields set to undefined [in the argument] are removed. [Other fields, that exist in the document but not in the argument, are unchanged]
I can see how the existing wording makes it sound like all existing fields in the document are overwritten or deleted, but that's not the intention. Happy patching!
StoicWanderer
StoicWandererOP14mo ago
Thank you so much for this, saved the day!!! How can i mark this as solved on mobile?

Did you find this page helpful?