Search code examples
node.jsfirebasegoogle-cloud-firestore

Firestore set doc with merge option true or update doc


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?


Solution

  • 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.