1
set with merge will update fields in the document or create it if it doesn't exists

update will update fields but will fail if the document doesn't exist

Wouldn't it be much easier to always use set merges?

Are the prices slightly different?

dontknowhy
  • 2,480
  • 2
  • 23
  • 67

2 Answers2

1

Between set merge and update there is difference in the use case.

You may find detailed information regarding this on this post.

Regarding the pricing, as stated here:

Each set or update operation counts as a single write and is being billed according to the region.

=========================================================================

EDIT:

The choice of which operation to use is greatly depending on the use case, as if you use "set merge" for a batch update, your request will successfully update all existing documents but also create dummy documents for non existent ids, which sometimes is not what you want.

After investigating a bit further, we could add another difference:

  • set merge will always override the data with the data you pass, while

  • update is specifically designed to give you the possibility to perform a partial update of a document without the possibility of creating incomplete documents that your code isn't otherwise prepared to handle. Please check this answer, as well as this scenario.

  • Thanks Aretemis. Yes I have seen that post . But my question is, are there really nothing internal differences? Only the query statement is slightly different? If you always use "set merge", You don't have to worry about whether to use an update. So it is much more convenient and there is no difference in function. Why did the firebase team make "update"? I can't understand really – dontknowhy Jan 27 '21 at 23:40
  • More specifically, "set merge" also doing kind of null checking, which "update" cannot. When "set merge", if null, error is prevented by overwriting. If "set merge" exactly doing same thing which "update" does in functionality, the Firebase team should delete the "update." This is because "set merge" is much higher compatible and has the same price. – dontknowhy Jan 28 '21 at 00:01
  • I added an "Edit" in my answer with some further information I could find. – Artemis Georgakopoulou Jan 28 '21 at 11:51
  • Can you more explain about "but also create dummy documents for non existent ids" this ? I don`t get it. And secondly, from the stackoverflow link answer you gave me new (in EDIT ANSWER). "Sam Stern" also sees SetOptions.merge() as a partial update rather than overwrite. What do you think? – dontknowhy Jan 28 '21 at 12:25
  • 1
    It falls under the process of when using set merge to update it will create a document if it does not exist. You may correlate "set merge" vs "update" to "PUT" method vs "PATCH" method. I found [this article](https://medium.com/backticks-tildes/restful-api-design-put-vs-patch-4a061aa3ed0b) highlighting this difference. – Artemis Georgakopoulou Jan 28 '21 at 12:28
  • Oh .. I see.. so it means querying price in Firestore are same but difference pricing in "Networking Price" (like a bandwith). "a PUT request always contains a full resource." This is because their data sizes are different. " right? PUT and PATCH basically have very little speed difference and very little network cost difference, right? – dontknowhy Jan 28 '21 at 12:44
  • Hm.. are you sure of it? I think PUT is an overwrite of "set" in Firestore, and PATCH is "update" and "set merge" in Firestore. In the first place, when doing "set merge", it seems that it is correct to see this as PATCH rather than as PUT, as it functions as an update even if you write down one field instead of several fields. – dontknowhy Jan 28 '21 at 14:09
  • 1
    You are right regarding the mapping of these operations, as PUT would replace the resource (similar to set WITHOUT merge) and PATCH would be similar to update. Hence, set with merge is somewhere in between, however regardless of mapping the operations to the verbs, set WITH merge is basically an "upsert" (update / insert), which is essentially different from update only. – Artemis Georgakopoulou Feb 01 '21 at 15:47
  • 1
    wow.. really? there is a "upsert"?? its really good to know – dontknowhy Feb 02 '21 at 00:37
0

The difference is that .set(data, {merge:true}) will update the document if it exists, or create the document if it doesn't.

.update() fails if the document doesn't exist.

But why does .update() still exist? Well, probably for backward compatibility. I believe .set() with merge:true has been introduced at a later date than .update(). As you have pointed out, set/merge is more versatile. I use it instead of .update() and instead of .add()

  • 1
    thanks dude. There was a case where the Firebase team did not create "update" and "set merge" at the same time, but set merge was supported later! But Firebase Team didn't delete it because of the people who were using it already! Isn't there really anything other than this reason? I'll wait for more answers and adopt this answer later if there are no more answers. – dontknowhy Jan 28 '21 at 10:11