Update a field in an object in Firestore?

This is the example provided in the documentation to update a field within a nested object in firebase.

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"
})
.then(function() {
console.log("Document successfully updated!");
});

Instead of updating the favourites, I want to add to favourites would someone point me in the right direction on how to do this.

Let’s say I want to

firebase: "Help"
the the resulting favourites object should be
favorites: { food: "Pizza", color: "Blue", subject: "recess", firebase: "Help" },

I used set with the dot operation but it overrides everything instead.

To add an entry to a set (a Javascript Object), use DocumentReference.update:

db.collection("users").doc("frank").update({
  "favorites.firebase": "Help")}
})

will result in

favorites: { food: "Pizza", color: "Blue", subject: "recess", firebase: "Help" }


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .
Read More:   Is there a way to catch the back button event in javascript?

Similar Posts