Setup - Firestore Composite Indexes (All Platforms)
Step 10: Configure Firestore Composite Indexes
To enable efficient querying in Firestore, composite indexes are required for certain collections like emails, homework, and notifications. This ensures that the app can fetch data dynamically based on the logged-in user's role.
Why is This Required?
- Firestore does not allow complex queries involving
arrayContains
andorderBy
together without an index. - Indexing improves query performance, allowing users to retrieve data quickly.
- Since this app is designed as a single common platform for all user roles (Admin, Staff, and Parents), indexes allow dynamic role-based data retrieval.
How to Add Composite Indexes?
Follow these steps to create the required indexes in Firestore:
A. Go to Firestore Indexes
Navigate to Firebase Console > Firestore Database > Indexes.
B. Add Indexes
Click on Add Index and add the following indexes:
- Emails Collection:
recipients (Array)
+timestamp (Descending)
- Homework Collection:
class (Ascending)
+section (Ascending)
+upload_date (Descending)
- Notifications Collection:
userId (Ascending)
+timestamp (Descending)
C. Verify Index Status
Once added, Firestore will start building the indexes, which might take a few minutes. To check the status:
- Go to Firestore Console > Indexes.
- Look under Composite Indexes to see if the indexes are active.
D. Restart Your App
Once the indexes are ready, restart your app to ensure that queries work correctly.
E. Screenshot of Created Indexes
