📖 Scenario: You are building a simple app that stores user profiles. You want to use Firebase's main database service to save and retrieve user data efficiently.
🎯 Goal: Learn how to set up Firestore as the primary database in Firebase and understand why it is preferred over other options.
📋 What You'll Learn
Create a Firestore database instance
Configure Firestore with proper settings
Add a user profile document to Firestore
Retrieve and display user profile data
💡 Why This Matters
🌍 Real World
Apps need a fast, scalable database to store user data and Firestore provides this with easy setup and real-time updates.
💼 Career
Understanding Firestore is essential for Firebase developers building modern web and mobile apps.
Progress0 / 4 steps
1
Create a Firestore database instance
Create a variable called db and set it to the Firestore database instance using firebase.firestore().
Firebase
Hint
Use firebase.firestore() to get the Firestore database instance.
2
Configure Firestore with proper settings
Create a variable called settings and set it to an object with timestampsInSnapshots set to true. Then apply these settings to db using db.settings(settings).
Firebase
Hint
Firestore requires enabling timestamps in snapshots for better date handling.
3
Add a user profile document to Firestore
Use db.collection('users').doc('user1').set() to add a document with name set to 'Alice' and age set to 30.
Firebase
Hint
Use collection and doc to specify where to store the user data.
4
Retrieve and display user profile data
Use db.collection('users').doc('user1').get() to retrieve the document, then access its data with doc.data().
Firebase
Hint
Use get() to fetch the document and data() to read its contents.
Practice
(1/5)
1. Why is Firestore considered Firebase's primary database?
easy
A. Because it only stores data temporarily
B. Because it requires manual scaling for large apps
C. Because it updates data in real time and works offline
Firestore automatically updates data in real time, so apps stay current without manual refresh.
Step 2: Recognize offline support and automatic scaling
Firestore works offline and scales automatically, making it easy to use and reliable.
Final Answer:
Because it updates data in real time and works offline -> Option C
Quick Check:
Real-time + offline = Firestore primary [OK]
Hint: Remember Firestore updates live and works offline [OK]
Common Mistakes:
Thinking Firestore needs manual scaling
Believing Firestore lacks security rules
Confusing Firestore with temporary storage
2. Which of the following is the correct way to define a Firestore collection reference in Firebase SDK?
easy
A. const ref = firestore.collection('users');
B. const ref = firestore.getCollection('users');
C. const ref = firestore.users.collection();
D. const ref = firestore.collection.users();
Solution
Step 1: Recall Firestore collection syntax
The correct method to get a collection reference is firestore.collection('collectionName').
Step 2: Check each option's syntax
Only const ref = firestore.collection('users'); uses the correct method name and syntax.
Final Answer:
const ref = firestore.collection('users'); -> Option A
Quick Check:
Use .collection('name') to get collection [OK]
Hint: Use .collection('name') to get Firestore collections [OK]
Common Mistakes:
Using getCollection instead of collection
Accessing collection as a property
Calling collection as a method on collection
3. Given this Firestore query code, what will be the output if the 'users' collection has documents with fields {name: 'Alice', age: 30} and {name: 'Bob', age: 25}?