0
0
Firebasecloud~20 mins

Document-collection data model in Firebase - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Firestore Data Model Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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);
A[{name: 'Alice'}, {name: 'Bob'}]
B[{id: 'user1', name: 'Alice', age: 30}, {id: 'user2', name: 'Bob', age: 25}]
C[{name: 'Alice', age: 30}, {name: 'Bob', age: 25}]
D[]
Attempts:
2 left
💡 Hint
The doc.data() method returns only the fields inside the document, not the document ID.
🧠 Conceptual
intermediate
1:30remaining
Understanding subcollections in Firestore
Which statement correctly describes subcollections in Firestore?
ASubcollections are only allowed at the root level of the database.
BSubcollections are nested inside documents and can have their own documents and collections.
CSubcollections are stored as fields inside a document as arrays.
DSubcollections automatically merge with parent collections when queried.
Attempts:
2 left
💡 Hint
Think about how Firestore organizes data hierarchically.
📝 Syntax
advanced
1:30remaining
Correct Firestore document reference syntax
Which option correctly gets a reference to a document with ID 'doc1' inside the 'orders' collection?
Aconst docRef = db.collection('orders').doc('doc1');
Bconst docRef = db.doc('orders').collection('doc1');
Cconst docRef = db.collection('orders/doc1');
Dconst docRef = db.doc('orders/doc1');
Attempts:
2 left
💡 Hint
Remember the method to get a document inside a collection.
optimization
advanced
2:00remaining
Efficiently querying nested subcollections
You want to retrieve all documents from the 'comments' subcollection under multiple 'posts' documents. Which approach is most efficient?
AUse a join query between 'posts' and 'comments' collections.
BStore all comments in a single top-level collection instead of subcollections.
CQuery each 'comments' subcollection under every 'posts' document separately.
DUse a collection group query on 'comments' to get all comments across posts.
Attempts:
2 left
💡 Hint
Firestore supports special queries to search across subcollections with the same name.
🔧 Debug
expert
2: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});
ANo error; document is added successfully.
BReferenceError: db is not defined.
CFirebaseError: Missing or insufficient permissions.
DTypeError: collection is not a function.
Attempts:
2 left
💡 Hint
Check if the code syntax is correct and if the database reference is valid.