Challenge - 5 Problems
Firestore Data Model Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Retrieve all documents in a collection
Given a Firestore collection named users with documents containing fields name and age, what will be the output of this query?
Firebase
const snapshot = await db.collection('users').get(); const result = snapshot.docs.map(doc => doc.data()); console.log(result);
Attempts:
2 left
💡 Hint
The
doc.data() method returns only the fields inside the document, not the document ID.✗ Incorrect
The query fetches all documents in the 'users' collection. Mapping with
doc.data() returns an array of objects with only the fields, excluding document IDs.🧠 Conceptual
intermediate1:30remaining
Understanding subcollections in Firestore
Which statement correctly describes subcollections in Firestore?
Attempts:
2 left
💡 Hint
Think about how Firestore organizes data hierarchically.
✗ Incorrect
Subcollections are separate collections nested under a document. They can contain their own documents and further subcollections, allowing hierarchical data organization.
📝 Syntax
advanced1:30remaining
Correct Firestore document reference syntax
Which option correctly gets a reference to a document with ID 'doc1' inside the 'orders' collection?
Attempts:
2 left
💡 Hint
Remember the method to get a document inside a collection.
✗ Incorrect
To get a document reference, use
collection('name').doc('id'). Option A is valid syntax but less common; however, in Firebase SDK, db.doc('orders/doc1') is also valid. To avoid ambiguity, only option A is accepted here as it is the standard approach.❓ optimization
advanced2:00remaining
Efficiently querying nested subcollections
You want to retrieve all documents from the 'comments' subcollection under multiple 'posts' documents. Which approach is most efficient?
Attempts:
2 left
💡 Hint
Firestore supports special queries to search across subcollections with the same name.
✗ Incorrect
Collection group queries allow querying all subcollections with the same name across the database efficiently, avoiding multiple queries.
🔧 Debug
expert2:30remaining
Identify the error in nested document creation
What error will this code produce when trying to add a document to a subcollection?
Firebase
await db.collection('users').doc('user1').collection('orders').add({item: 'book', price: 10});
Attempts:
2 left
💡 Hint
Check if the code syntax is correct and if the database reference is valid.
✗ Incorrect
The code correctly references a subcollection and adds a document. If permissions are set correctly and db is defined, no error occurs.