0
0
Firebasecloud~5 mins

Data aggregation patterns in Firebase - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you have many pieces of data spread out, it can be slow and costly to add them up every time you want a total. Data aggregation patterns help you keep track of sums or counts in one place so you can get results quickly.
When you want to show the total number of likes on a post without counting every like each time.
When you need to display the average rating of a product updated in real-time.
When you want to keep track of how many users are active in a chat room.
When you want to quickly get the total sales amount without scanning all sales records.
When you want to reduce the number of reads to your database to save costs.
Config File - firestore.rules
firestore.rules
rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /posts/{postId} {
      allow read, write: if true;
    }
    match /posts/{postId}/likes/{likeId} {
      allow read, write: if true;
    }
    match /aggregates/{postId} {
      allow read, write: if true;
    }
  }
}

This Firestore security rules file allows reading and writing to posts, likes subcollections, and aggregate documents. It ensures your app can update and read aggregated data safely.

Commands
Initialize Firestore in your Firebase project to start using the database and set up rules and indexes.
Terminal
firebase init firestore
Expected OutputExpected
✔ Firestore: Rules file firestore.rules already exists, skipping. ✔ Firestore: Indexes file firestore.indexes.json already exists, skipping. Firebase initialization complete!
Deploy the Firestore security rules to protect your data and allow aggregation updates.
Terminal
firebase deploy --only firestore:rules
Expected OutputExpected
=== Deploying to 'your-project-id'... ✔ firestore: rules firestore.rules deployed successfully ✔ Deploy complete!
Set a configuration flag for your cloud functions to enable aggregation logic.
Terminal
firebase functions:config:set firestore.aggregate=true
Expected OutputExpected
✔ Functions config updated.
Deploy cloud functions that update aggregate counts or sums when data changes.
Terminal
firebase deploy --only functions
Expected OutputExpected
=== Deploying to 'your-project-id'... ✔ functions: all functions deployed successfully ✔ Deploy complete!
Key Concept

If you remember nothing else from this pattern, remember: keep running totals updated as data changes to get fast, cost-effective aggregated results.

Common Mistakes
Reading all individual documents every time you want a total.
This causes slow responses and high costs because it reads too much data repeatedly.
Use aggregation documents that update totals incrementally when data changes.
Not securing aggregate documents with proper rules.
Anyone could overwrite or delete your totals, causing wrong data to show.
Write Firestore security rules to protect aggregate documents and allow only trusted updates.
Updating aggregates outside of transactions or cloud functions.
This can cause race conditions and incorrect totals if multiple updates happen at once.
Use Firestore transactions or cloud functions triggered on data changes to update aggregates safely.
Summary
Initialize Firestore and set up security rules to allow safe data and aggregate updates.
Deploy cloud functions that update aggregate documents when underlying data changes.
Use aggregate documents to store running totals or counts for fast and cheap reads.