Firestore: difference between set() and add()

19,055

Since you didn't specify, I'm going to assume you mean set() on DocumentReference, and add() on CollectionReference.

When you use set() on a DocumentReference, you're putting data into a document that you're already identified by some unique id. (Otherwise, you wouldn't already have a DocumentReference object!) As it says in the docs, "If the document does not yet exist, it will be created." If the document already exists, you're either replacing or merging new data into it.

When you use add() on a CollectionReference, you're unconditionally creating a new document in a collection, and that new document will have a unique id assigned to it. The data you pass will become the contents of the new document.

Share:
19,055
Crazzi_Boii
Author by

Crazzi_Boii

Updated on June 06, 2022

Comments

  • Crazzi_Boii
    Crazzi_Boii about 2 years

    What is the difference between set() and add() in Firestore?

    I use the set() to add documents to my collection. But I am unable to use add() or understand add() from docs.

  • Crazzi_Boii
    Crazzi_Boii over 6 years
    Thank you @Doug for this short of clarification.