firebase firestore merge code example

Example 1: set firestore merge

The way I understood the difference:

set without merge will overwrite a document or create it if it doesn't exist yet

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

create will create the document but fail if the document already exists

There's also a difference in the kind of data you provide to set and update.

For set you always have to provide document-shaped data:

set(
  {a: {b: {c: true}}},
  {merge: true}
)
With update you can also use field paths for updating nested values:

update({
  'a.b.c': true
})

Example 2: firebase cloud method update

firebase deploy --only functions:functionNameHere

Example 3: document.set() firebasefirestore java

// Create an initial document to update.var frankDocRef = db.collection("users").doc("frank");frankDocRef.set({    name: "Frank",    favorites: { food: "Pizza", color: "Blue", subject: "recess" },    age: 12});// To update age and favorite color:db.collection("users").doc("frank").update({    "age": 13,    "favorites.color": "Red"});

Tags:

C Example