How do `set` and `update` differ in Firebase Realtime Database?

I’m working on a project using Firebase and I’m confused about when to use set versus update in the Realtime Database API. I’ve looked at the docs but the examples don’t really clarify the difference. It seems like they do the same thing.

Here’s a quick example of what I mean:

function addNewItem(itemId, name, price) {
  const itemData = {
    id: itemId,
    name: name,
    price: price,
    inStock: true
  };

  // Using update
  const updates = {};
  updates['/items/' + itemId] = itemData;
  updates['/categories/all/' + itemId] = itemData;
  firebase.database().ref().update(updates);

  // Using set
  firebase.database().ref('/items/' + itemId).set(itemData);
  firebase.database().ref('/categories/all/' + itemId).set(itemData);
}

Both seem to work the same way. Can someone explain when I should use one over the other? Are there any performance differences or other considerations I should keep in mind?

yo, i’ve been there too! set is like a bulldozer, wipes everything clean. update is more chill, just tweaks what u tell it to. i usually go with update cuz it’s safer and faster for multiple changes. plus, u never know when you might need those other fields later. keep it flexible, ya know?

I’ve been using Firebase for a while now, and I can tell you from experience that the choice between set and update can make a big difference in how your app behaves.

set is like a sledgehammer - it completely replaces whatever’s at that location. I learned this the hard way when I accidentally wiped out a bunch of user data because I forgot to include all the fields in my set operation. Oops!

update, on the other hand, is more surgical. It only touches the fields you specify, which is great for partial updates. I use it all the time for things like updating a user’s status or incrementing a counter without messing with other data.

One thing to keep in mind is that update can be more efficient if you’re changing multiple paths at once, like in your example. It bundles all those changes into one network request, which can be a nice performance boost.

My rule of thumb: use set for creating new records or when you’re absolutely sure you want to replace everything, and update for partial changes or when you’re touching multiple paths. It’s saved me a lot of headaches!

The key difference between set and update lies in how they handle existing data. set completely overwrites the data at the specified location, while update only modifies the specified fields, leaving others untouched.

In your example, using set would replace all existing data for that item, potentially erasing fields not included in itemData. update, on the other hand, allows for partial updates, which can be more efficient when dealing with large objects or multiple paths.

Performance-wise, update is generally more efficient for multiple path updates, as it batches changes into a single write operation. This reduces network overhead and ensures atomicity.

For your use case, update seems more appropriate, especially if you might need to modify only certain fields in the future without affecting others. It provides more flexibility and can help prevent accidental data loss.