In Firebase Firestore, what happens when you use add() compared to set() without specifying a document ID?
Think about which method is designed for quick document creation without manual ID management.
The add() method creates a new document with a unique ID generated by Firestore automatically. The set() method requires you to specify the document ID explicitly when creating a document.
When using set() in Firestore, how can you update only specific fields without overwriting the entire document?
Check the options object passed to set() for controlling overwrite behavior.
By default, set() overwrites the entire document. Passing { merge: true } merges the new data with existing fields, updating only specified fields.
You are designing a Firestore database to store user comments. Users can add comments freely. Which method is best to add new comments to the comments collection and why?
Consider which method simplifies adding many documents without manual ID management.
Using add() is best for user-generated content like comments because it automatically generates unique IDs, avoiding conflicts and simplifying writes.
What is a potential security risk when clients use set() with client-generated document IDs in Firestore?
Think about how document IDs control access and data integrity.
If clients generate document IDs, they might guess IDs of other documents and overwrite or delete data they shouldn't access. This risk is reduced when using add() because IDs are generated server-side.
You need to create a Firestore document with a specific ID only if it does not already exist. Which approach ensures atomic creation without overwriting existing data?
Consider how to avoid race conditions when creating documents with known IDs.
Using a transaction allows you to atomically check if a document exists and create it only if it does not, preventing overwriting existing data. set() alone cannot guarantee this atomicity.