0
0
Firebasecloud~5 mins

Supported data types in Firebase - Commands & Configuration

Choose your learning style9 modes available
Introduction
Firebase stores data in a flexible way using different types. Knowing these types helps you save and read data correctly without errors.
When you want to save a user's name as text in your app database
When you need to store a number like age or score for a player
When you want to save a list of items like shopping list or tasks
When you need to store true or false values for settings
When you want to save a date and time for events or logs
Commands
This command creates a Firestore document for user1 with different data types: string, number, boolean, array, and timestamp.
Terminal
firebase firestore:documents:create users/user1 --data '{"name":"Alice","age":30,"isActive":true,"tags":["admin","user"],"lastLogin":{"seconds":1625097600,"nanoseconds":0}}'
Expected OutputExpected
Document users/user1 created successfully.
--data - Specifies the data to store in the document using JSON format
This command retrieves the document for user1 to verify the stored data types and values.
Terminal
firebase firestore:documents:get users/user1
Expected OutputExpected
Document data for users/user1: { "name": "Alice", "age": 30, "isActive": true, "tags": ["admin", "user"], "lastLogin": { "seconds": 1625097600, "nanoseconds": 0 } }
Key Concept

If you remember nothing else from this pattern, remember: Firebase supports strings, numbers, booleans, arrays, maps, and timestamps as main data types to store your app data.

Common Mistakes
Trying to store a date as a plain string instead of a timestamp object
This causes problems when querying or sorting by date because Firebase treats it as text, not time.
Use Firebase timestamp objects with seconds and nanoseconds fields to store dates.
Saving complex objects without using maps, just as strings
You lose the ability to query or update nested fields properly.
Use maps (nested JSON objects) to store structured data.
Summary
Use strings for text like names and descriptions.
Use numbers for counts, ages, or scores.
Use booleans for true/false settings.
Use arrays to store lists of values.
Use timestamps to store dates and times correctly.