Reputation: 668
I have to update a single field in an existed Firestore document.
However, I don't know if there is some difference between using
admin.firestore().doc('doc_id').set({name:'Bill'},{ merge: true })
or
admin.firestore().doc('doc_id').update({name:'Bill'})
If the document is:
{
name: Bart,
age: 18
}
Should .update(...)
just update the field and not remove the field "age" in this case?
Upvotes: 12
Views: 3257
Reputation: 81
I'm not sure if it worked like this before but.
setDoc
with merge
will also merge child objects, when updateDoc
overwrites them.
I mean if we have a document like:
{
child: {
a: 'a'
}
}
And after setDoc(ref, { child: { b: 'b' } }, { merge: true })
we will have a document like:
{
child: {
a: 'a',
b: 'b'
}
}
updateDoc(ref, { child: { b: 'b' } })
results in:
{
child: {
b: 'b'
}
}
Upvotes: 1
Reputation: 317692
There is no difference between those two options for existing documents.
The difference between them is only evident for documents that don't exist. set()
with merge will create the document if it does't exist, and update()
will fail if it doesn't exist.
Upvotes: 15