Hint: add() returns promise; await before doc() calls. [OK]
Common Mistakes:
Chaining doc() immediately after add()
Ignoring promise returned by add()
Assuming set() cannot update fields
5. You want to create a collection 'orders' and add a document with a specific ID 'order123' containing {item: 'Pen', quantity: 3}. Which code snippet is correct?
hard
A. db.collection('orders').doc('order123').set({item: 'Pen', quantity: 3});
B. db.collection('orders').add({item: 'Pen', quantity: 3, id: 'order123'});
C. db.collection('orders').doc().set({item: 'Pen', quantity: 3, id: 'order123'});
D. db.collection('orders').set('order123', {item: 'Pen', quantity: 3});
Solution
Step 1: Identify method to create document with specific ID
Use doc('order123') to specify the document ID explicitly.
Step 2: Use set() to create or overwrite document data
set() writes the data to the specified document ID.
Final Answer:
db.collection('orders').doc('order123').set({item: 'Pen', quantity: 3}); -> Option A
Quick Check:
Specific ID document creation = doc(id).set() [OK]
Hint: Use doc('id').set() for specific document IDs. [OK]
Common Mistakes:
Using add() with id field (does not set document ID)
Calling doc() without ID for specific ID
Using set() directly on collection
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.