Catalog / Firestore Cheatsheet
Firestore Cheatsheet
A comprehensive guide to Google Firestore, covering data modeling, querying, security rules, and common operations. Ideal for developers building scalable, real-time applications.
Data Modeling & Structure
Collections & Documents
Collections |
Containers for documents. Names are strings and must be unique within the database. |
Documents |
Contain data as key-value pairs. Can contain nested objects and arrays. |
Document ID |
Unique identifier for a document within a collection. Can be auto-generated or user-defined. |
Subcollections |
Collections nested within documents, useful for hierarchical data structures. |
Path |
Structure |
Data Types
String |
Textual data. |
Number |
Integers and floating-point numbers. |
Boolean |
|
Timestamp |
Point in time (seconds and nanoseconds). |
GeoPoint |
Latitude and longitude coordinates. |
Array |
Ordered list of values. |
Map |
Nested key-value pairs (object). |
Null |
Represents a missing or undefined value. |
Reference |
Pointer to another document in Firestore. |
Best Practices
Favor denormalization to optimize read performance. Minimize subcollections to avoid complex queries. |
Use batched writes for multiple operations to improve efficiency. |
Structure data to fit query patterns; consider compound indexes. |
Querying Data
Basic Queries
|
Filters documents based on field values. Supports |
|
Sorts the results by a specific field. Can specify ascending or descending order. |
|
Limits the number of documents returned. |
|
Paginates results, starting at a specific document or value. |
|
Paginates results, ending at a specific document or value. |
Compound Queries
Combine multiple |
Example: |
Collection Group Queries
Query across all collections with the same ID, regardless of their location in the database. Useful for retrieving data from deeply nested subcollections. Requires enabling collection group index. |
Example: |
Limitations
Firestore does not support |
Inequality and range queries are limited to a single field, unless you enable multiple range filters. |
Security Rules
Basic Syntax
|
Specifies the version of the rules syntax. |
|
Declares the service to which the rules apply. |
|
Defines the database path to which the rules apply. Can use wildcards. |
|
Grants read or write access if the specified condition is met. |
Common Conditions
|
Requires authentication. |
|
Checks if the authenticated user’s ID matches a specific user ID. |
|
Compares the value of a field in the incoming data to a specific value. |
|
Verifies user’s role stored in their profile document |
|
Checks if a document exists at a specific path. |
Example Rules
|
Testing Rules
Use the Firestore Rules Simulator in the Firebase console to test your rules before deploying them. |
Simulate various scenarios, including authenticated and unauthenticated users, and different data values. |
Common Operations
CRUD Operations
Create |
|
Read |
|
Update |
|
Delete |
|
Transactions
Ensure atomicity and consistency when performing multiple operations. Transactions automatically retry if conflicts occur. |
|
Batched Writes
Perform multiple write operations as a single atomic unit. More efficient than individual writes. |
|
Realtime Updates
Listen for changes to documents or collections and receive updates in real-time. |
|