📖 Scenario: You are building a simple Firebase Firestore database to store user profiles for a mobile app. Each user profile will have different types of data such as text, numbers, dates, and boolean values.
🎯 Goal: Create a Firestore document with fields using the supported Firebase data types: string, number, boolean, timestamp, and array.
📋 What You'll Learn
Create a Firestore document data dictionary with specific fields and values
Add a configuration variable for the current timestamp
Use the Firestore data types correctly in the document data
Complete the Firestore document creation code with the correct collection and document ID
💡 Why This Matters
🌍 Real World
Storing user profiles with various data types is common in mobile and web apps using Firebase Firestore.
💼 Career
Understanding supported data types and how to write documents is essential for backend and cloud developers working with Firebase.
Progress0 / 4 steps
1
Create Firestore document data with basic fields
Create a dictionary called user_profile with these exact fields and values: 'name': 'Alice', 'age': 30, 'is_active': true
Firebase
Hint
Use Python dictionary syntax with exact keys and values.
2
Add a timestamp field for the current time
Import datetime from datetime module and create a variable called current_time set to datetime.datetime.now()
Firebase
Hint
Use datetime.now() to get the current timestamp.
3
Add timestamp and array fields to the document data
Add a new key 'last_login' with value current_time and a key 'favorite_colors' with value ['red', 'green', 'blue'] to the user_profile dictionary
Firebase
Hint
Add the new keys and values inside the user_profile dictionary.
4
Complete Firestore document creation code
Import firestore from firebase_admin, initialize the client as db = firestore.client(), and write the user_profile dictionary to the collection 'users' with document ID 'alice123' using db.collection('users').document('alice123').set(user_profile)
Firebase
Hint
Use Firestore client to write the document with the exact collection and document ID.
Practice
(1/5)
1. Which of the following is NOT a supported data type in Firebase Firestore?
easy
A. String
B. Function
C. Boolean
D. GeoPoint
Solution
Step 1: Review Firebase supported data types
Firebase supports strings, booleans, numbers, arrays, maps, timestamps, and geopoints.
Step 2: Identify unsupported type
Functions are not stored as data types in Firestore; they are part of code logic, not data.
Final Answer:
Function -> Option B
Quick Check:
Functions are not data types in Firestore [OK]
Hint: Remember: Firestore stores data, not code like functions [OK]
Common Mistakes:
Confusing code functions with data types
Thinking all JavaScript types are supported
Assuming Firestore stores executable code
2. Which is the correct way to store a timestamp in Firebase Firestore?
easy
A. Store as a number representing milliseconds since epoch
B. Use a string with date format like '2024-06-01'
C. Use the Firebase Timestamp type
D. Use a boolean true or false
Solution
Step 1: Understand Firestore timestamp storage
Firestore has a special Timestamp type to store date and time accurately.
Step 2: Compare options
Strings and numbers can store dates but lack Firestore's timestamp features; booleans are unrelated.
Final Answer:
Use the Firebase Timestamp type -> Option C
Quick Check:
Timestamp type stores dates best [OK]
Hint: Use Firebase Timestamp for dates, not strings or numbers [OK]
Common Mistakes:
Storing dates as plain strings
Using numbers without Timestamp type
Confusing booleans with dates
3. What will be the data type of the field location after this Firestore document is saved?
const docData = { location: new firebase.firestore.GeoPoint(40.7128, -74.0060) };
medium
A. GeoPoint
B. Array
C. String
D. Map
Solution
Step 1: Identify the data type used
The code uses new firebase.firestore.GeoPoint() which creates a GeoPoint object.
Step 2: Understand Firestore storage
Firestore stores GeoPoint objects as GeoPoint type, representing geographic coordinates.
Final Answer:
GeoPoint -> Option A
Quick Check:
GeoPoint stores latitude and longitude [OK]
Hint: GeoPoint objects store coordinates, not strings or arrays [OK]
Common Mistakes:
Thinking GeoPoint is stored as string
Assuming coordinates are arrays
Confusing GeoPoint with map type
4. You try to save this data in Firestore:
{ name: 'Alice', age: '25' }
But you want age to be a number. What is the error and how to fix it?
medium
A. No error, Firestore converts string to number automatically
B. No error, but age should be stored as boolean true
C. Error: Firestore does not allow strings, use boolean instead
D. Error: age is string, fix by storing age as number 25
Solution
Step 1: Check data types in the object
Age is stored as a string '25', not a number 25.
Step 2: Understand Firestore type strictness
Firestore stores data as given; it does not convert string to number automatically.
Final Answer:
Error: age is string, fix by storing age as number 25 -> Option D
Quick Check:
Store numbers as numbers, not strings [OK]
Hint: Store numbers as numbers, not strings to avoid type errors [OK]
Common Mistakes:
Assuming Firestore auto-converts types
Using booleans for numbers
Ignoring type mismatch warnings
5. You want to store a user's profile with name, age, location (latitude and longitude), and signup date in Firestore. Which data types should you use for location and signupDate fields?
hard
A. location as GeoPoint, signupDate as Timestamp
B. location as Map with lat/lng numbers, signupDate as string
C. location as Array of numbers, signupDate as number (milliseconds)
D. location as string 'lat,lng', signupDate as boolean true
Solution
Step 1: Identify best types for location and date
Firestore provides GeoPoint for geographic coordinates and Timestamp for dates.
Step 2: Evaluate options
Using GeoPoint and Timestamp ensures correct storage, querying, and indexing.
Final Answer:
location as GeoPoint, signupDate as Timestamp -> Option A
Quick Check:
Use GeoPoint and Timestamp for location and dates [OK]
Hint: Use GeoPoint for locations and Timestamp for dates [OK]
Common Mistakes:
Storing dates as strings
Using arrays for location
Confusing booleans with dates
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.