0
0
Firebasecloud~20 mins

Adding documents (add vs set) in Firebase - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Firestore Document Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
service_behavior
intermediate
2:00remaining
Difference in document ID generation between add() and set()

In Firebase Firestore, what happens when you use add() compared to set() without specifying a document ID?

ABoth add() and set() automatically generate unique document IDs if none is specified.
Badd() automatically generates a unique document ID; set() requires you to specify the document ID explicitly.
Cadd() requires you to specify the document ID; set() automatically generates a unique document ID.
DNeither add() nor set() can create documents without specifying a document ID.
Attempts:
2 left
💡 Hint

Think about which method is designed for quick document creation without manual ID management.

Configuration
intermediate
2:00remaining
Using set() to overwrite vs merge document data

When using set() in Firestore, how can you update only specific fields without overwriting the entire document?

AUse set() without any options; it automatically merges fields.
BUse add() instead of set() to update specific fields without overwriting.
CUse set() with the option <code>{ merge: true }</code> to update specific fields without overwriting the whole document.
DUse set() with the option <code>{ overwrite: false }</code> to update specific fields.
Attempts:
2 left
💡 Hint

Check the options object passed to set() for controlling overwrite behavior.

Architecture
advanced
2:30remaining
Choosing add() vs set() for user-generated content

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?

AUse add() but manually generate IDs to control comment IDs.
BUse set() with a user-generated ID to keep track of comments by user ID.
CUse set() with a timestamp as the document ID to order comments chronologically.
DUse add() to let Firestore generate unique IDs automatically, ensuring no ID conflicts and easy scalability.
Attempts:
2 left
💡 Hint

Consider which method simplifies adding many documents without manual ID management.

security
advanced
2:30remaining
Security implications of using set() with client-generated IDs

What is a potential security risk when clients use set() with client-generated document IDs in Firestore?

AClients might overwrite or delete documents they should not have access to by guessing document IDs.
BClients cannot write data without server approval, so no risk exists.
CUsing client-generated IDs causes Firestore to reject writes automatically.
DThere is no difference in security risk between add() and set() with client-generated IDs.
Attempts:
2 left
💡 Hint

Think about how document IDs control access and data integrity.

Best Practice
expert
3:00remaining
Best practice for atomic document creation with known ID

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?

AUse a transaction to check if the document exists, then create it with set() if it does not.
BUse set() with <code>{ merge: true }</code> to create or update the document safely.
CUse add() with the specific ID passed as a parameter to create the document.
DUse set() without options; it will create the document only if it does not exist.
Attempts:
2 left
💡 Hint

Consider how to avoid race conditions when creating documents with known IDs.