Answers for "firestore set or update"

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
})
Posted by: Guest on February-12-2021
1

firestore create document with auto id

// Add a new document with a generated id.
db.collection("cities").add({
    name: "Tokyo",
    country: "Japan"
})
.then(function(docRef) {
    console.log("Document written with ID: ", docRef.id);
})
.catch(function(error) {
    console.error("Error adding document: ", error);
});
---------------------------
//If you want to specify id then use .set() 
// e.x   /
db.collection("cities").doc("TYO").set({
    name: "Tokyo",
    country: "Japan"
})
Posted by: Guest on January-16-2021
0

document.set() firebasefirestore java

var washingtonRef = db.collection("cities").doc("DC");
// Atomically add a new region to the "regions" array field.
washingtonRef.update({
    regions: firebase.firestore.FieldValue.arrayUnion("greater_virginia")
});
// Atomically remove a region from the "regions" array field.
washingtonRef.update({
    regions: firebase.firestore.FieldValue.arrayRemove("east_coast")
});
Posted by: Guest on July-20-2020

Browse Popular Code Answers by Language