When updating a document in Firestore, I want to keep most of the document, but change one property which contains an object.
What I have
{
name: "John Doe",
email: "me@example.com",
friends: {
a: {...},
b: {...},
c: {...},
d: {...},
e: {...},
f: {...},
}
}
Now, I have a new friends object like {x: ..., y: ..., z: ...}
I want to overwrite the friends tree of my document, but keep all other fields.
What I want it to look like
{
name: "John Doe",
email: "me@example.com",
friends: {
x: {...},
y: {...},
z: {...},
}
}
However, if I do a firestore.doc(...).update({friends: {...}}, { merge: true })
What I currently get
{
name: "John Doe",
email: "me@example.com",
friends: {
a: {...},
b: {...},
c: {...},
d: {...},
e: {...},
f: {...},
x: {...},
y: {...},
z: {...},
}
}
I know that I can do two updates, i.e. delete the field and then set it again, or I can read the document, change the object and save it without merging.
But isn't there a smart way to overwrite an object (map) while keeping the rest of the document untouched?