Discover how a simple choice can save your data from being lost or overwritten!
Adding documents (add vs set) in Firebase - When to Use Which
Imagine you have a notebook where you write down important notes. You want to add a new note, but you have to decide whether to write it on a new page automatically or choose a specific page yourself.
Manually choosing pages or writing notes can be confusing and slow. You might accidentally overwrite an existing note or waste time finding a free page. This causes mistakes and frustration.
Using 'add' lets the system pick a new page for your note automatically, so you never overwrite anything. Using 'set' lets you choose exactly where to write, giving you control when you need it.
db.collection('notes').doc('note1').set({text: 'Hello'})
db.collection('notes').add({text: 'Hello'})
This choice lets you easily add new data safely or update specific data exactly as you want.
When saving user messages, 'add' quickly creates new entries without conflicts. When updating user profiles, 'set' updates the exact record you want.
'add' creates new documents with unique IDs automatically.
'set' writes data to a specific document ID you choose.
Choosing the right method prevents data loss and saves time.