What is a document in the Document-collection data model?
A document is a single record that stores data as key-value pairs, similar to a JSON object. It is the basic unit of storage in Firebase's document-collection model.
Click to reveal answer
beginner
What is a collection in Firebase's Document-collection data model?
A collection is a group of documents. It acts like a folder that holds multiple documents, but it does not store data itself.
Click to reveal answer
intermediate
How are documents and collections related in Firebase?
Documents live inside collections. Each collection contains many documents, and each document can contain subcollections, creating a tree-like structure.
Click to reveal answer
intermediate
Can documents contain other documents directly in Firebase?
No, documents cannot contain other documents directly. Instead, documents can contain subcollections, which then contain documents.
Click to reveal answer
beginner
Why is the Document-collection data model useful for apps?
It organizes data in a simple, flexible way that matches real-world objects. It allows easy access and updates to small pieces of data without loading everything.
Click to reveal answer
In Firebase, what does a collection contain?
AOther collections
BOnly strings
CDocuments
DFiles
✗ Incorrect
A collection contains documents, which are the main data units.
Which of these can a document contain directly?
ASubcollections
BOther documents
CCollections
DDatabases
✗ Incorrect
Documents can contain subcollections, not other documents directly.
How is data stored inside a document?
AAs a list of collections
BAs key-value pairs
CAs a single string
DAs a binary file
✗ Incorrect
Documents store data as key-value pairs, similar to JSON objects.
What is the best way to think about a collection?
ALike a folder holding documents
BLike a single data record
CLike a database server
DLike a file
✗ Incorrect
A collection is like a folder that holds many documents.
Can a document exist without being inside a collection?
AOnly if it is a root document
BYes, documents can stand alone
COnly in special cases
DNo, every document must be inside a collection
✗ Incorrect
Documents always live inside collections in Firebase.
Explain the relationship between documents and collections in Firebase's Document-collection data model.
Think of collections as folders and documents as files inside them.
You got /4 concepts.
Describe why the Document-collection data model is useful for organizing app data.
Consider how apps need to store and retrieve data efficiently.
You got /4 concepts.
Practice
(1/5)
1. In Firebase's document-collection data model, what is the main container that holds documents?
easy
A. A collection
B. A table
C. A row
D. A field
Solution
Step 1: Understand Firebase data structure
Firebase stores data in documents, and these documents are grouped inside collections.
Step 2: Identify the container for documents
The container that holds multiple documents is called a collection, not a table, row, or field.
Final Answer:
A collection -> Option A
Quick Check:
Documents are inside collections [OK]
Hint: Remember: collections hold documents, like folders hold files [OK]
Common Mistakes:
Confusing collections with tables from SQL
Thinking documents are inside fields
Mixing up rows with documents
2. Which of the following is the correct way to reference a document named user123 inside a collection users in Firebase?
easy
A. db.collection('users').doc('user123')
B. db.doc('users').doc('user123')
C. db.collection('user123').doc('users')
D. db.users('user123')
Solution
Step 1: Recall Firebase syntax for document reference
To get a document inside a collection, use collection('name').doc('id').
Step 2: Match the correct syntax
db.collection('users').doc('user123') uses db.collection('users').doc('user123'), which is the standard way.
Final Answer:
db.collection('users').doc('user123') -> Option A
Quick Check:
Use collection then doc to reference documents [OK]
Hint: Use collection('name').doc('id') to get a document [OK]
What will be printed in the console if the 'books' collection was empty before?
medium
A. []
B. ['book1']
C. ['Learn Firebase']
D. Error: Cannot read property 'title' of undefined
Solution
Step 1: Understand the code flow
The code adds a document 'book1' with title and pages to 'books' collection, then fetches all documents in 'books'.
Step 2: Analyze the output of mapping titles
Since 'books' was empty before, after adding 'book1', the collection has one document with title 'Learn Firebase'. Mapping titles returns ['Learn Firebase'].
Final Answer:
['Learn Firebase'] -> Option C
Quick Check:
Added one doc, so titles list has one title [OK]
Hint: After set(), collection has that document; map titles returns it [OK]
Common Mistakes:
Thinking snapshot is empty before set()
Confusing document ID with title
Expecting error due to async code
4. Consider this Firebase code snippet:
const userRef = db.collection('users').doc('user1');
userRef.get().then(doc => {
if (doc.exists) {
console.log(doc.data().name);
} else {
console.log('No such document!');
}
});
If the document 'user1' does not exist, what will be printed?
medium
A. undefined
B. 'No such document!'
C. Error: doc.data() is undefined
D. null
Solution
Step 1: Check the existence condition
The code checks if doc.exists is true before accessing data.
Step 2: Understand behavior when document missing
If the document does not exist, doc.exists is false, so it prints 'No such document!'.
Final Answer:
'No such document!' -> Option B
Quick Check:
doc.exists false triggers else block [OK]
Hint: Check doc.exists before accessing data to avoid errors [OK]
Common Mistakes:
Trying to access data without checking doc.exists
Expecting undefined or null instead of message
Confusing doc.exists with doc.data()
5. You want to store a list of users, each with multiple addresses, in Firebase. Which data model best fits this scenario?
hard
A. One document 'users' containing all users and their addresses as nested arrays
B. A single collection 'addresses' with documents containing user info and address combined
C. Multiple collections named after each user containing address documents
D. A collection 'users' with documents for each user, each containing a subcollection 'addresses' with address documents
Solution
Step 1: Understand hierarchical data in Firebase
Firebase supports subcollections inside documents, which is ideal for related nested data.
Step 2: Choose the best structure for users and their multiple addresses
Using a 'users' collection with each user document having an 'addresses' subcollection keeps data organized and scalable.
Final Answer:
A collection 'users' with documents for each user, each containing a subcollection 'addresses' with address documents -> Option D
Quick Check:
Use subcollections for nested related data [OK]
Hint: Use subcollections for nested lists like addresses per user [OK]
Common Mistakes:
Storing all users and addresses in one big document
Mixing user and address data in one collection without hierarchy
Creating many collections named after users
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.