Design Firestore Data Models
Intermediatev1.0.0
Master Firestore data modeling — document structure, subcollections, denormalization, composite indexes, and query optimization for scalable NoSQL databases.
Content
Overview
Firestore data modeling requires thinking in documents and collections instead of tables and rows. The key principle: structure data for your queries, not for normalized relationships. Denormalization and subcollections are your primary tools.
Why This Matters
- -Query performance — reads are proportional to result set, not collection size
- -Cost efficiency — fewer reads with denormalized data
- -Scalability — Firestore scales automatically with proper modeling
- -Simplicity — well-modeled data simplifies client code
How It Works
Step 1: Document & Collection Design
Step 2: Denormalize for Read Performance
Step 3: Use Subcollections for Unbounded Lists
Step 4: Composite Indexes
Step 5: Aggregation with Counters
Best Practices
- -Model for queries, not relationships (denormalize when needed)
- -Use subcollections for unbounded lists (comments, followers, activity)
- -Keep documents under 1MB (Firestore limit)
- -Maintain counters with Cloud Functions (not client-side transactions)
- -Pre-define composite indexes in firestore.indexes.json
- -Use collection group queries for cross-collection searches
Common Mistakes
- -Over-normalizing like a SQL database (requires expensive joins)
- -Storing unbounded arrays in documents (hits 1MB limit)
- -Not creating composite indexes (queries fail at runtime)
- -Client-side aggregations instead of server-side counters
- -Deep nesting subcollections (hard to query across collections)
FAQ
Discussion
Loading comments...