Query limitations and workarounds in Firebase - Time & Space Complexity
When using Firebase queries, some limits affect how many operations run as data grows.
We want to see how query limits impact the number of calls as data size increases.
Analyze the time complexity of paginated queries with limits.
const pageSize = 10;
let lastVisible = null;
async function getNextPage() {
let query = firestore.collection('items').orderBy('timestamp').limit(pageSize);
if (lastVisible) {
query = query.startAfter(lastVisible);
}
const snapshot = await query.get();
lastVisible = snapshot.docs[snapshot.docs.length - 1];
return snapshot.docs.map(doc => doc.data());
}
This code fetches data in pages of 10 items, using the last item as a cursor for the next page.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Querying a page of data with a limit and cursor.
- How many times: Once per page requested by the user.
Each page fetch makes one query call returning a fixed number of items.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 1 query call |
| 100 | 10 query calls (10 pages) |
| 1000 | 100 query calls (100 pages) |
Pattern observation: The number of query calls grows linearly with the number of pages requested.
Time Complexity: O(n)
This means the total number of queries grows directly with how many items you want to fetch.
[X] Wrong: "Fetching more data pages will still be just one query call."
[OK] Correct: Each page requires a separate query call, so more pages mean more calls.
Understanding how query limits affect calls helps you design efficient data fetching in real apps.
"What if we removed the limit and tried to fetch all data at once? How would the time complexity change?"
Practice
Solution
Step 1: Understand Firebase query filter limits
Firebase allows only one range filter (like greater than or less than) on a single field per query to maintain performance.Step 2: Recognize the impact on multiple filters
Applying multiple range filters on different fields is not allowed, so you must design queries accordingly.Final Answer:
You can only apply range filters on a single field at a time. -> Option AQuick Check:
Range filters limited to one field = B [OK]
- Thinking multiple range filters on different fields are allowed
- Assuming no index setup is needed for filters
- Believing unlimited filters can be combined
age is greater than 18 in Firebase Firestore?Solution
Step 1: Recall Firestore query syntax
The correct method to filter documents is usingwhere(field, operator, value).Step 2: Match the correct operator and syntax
The operator for greater than is '>', sowhere('age', '>', 18)is correct.Final Answer:
db.collection('users').where('age', '>', 18) -> Option BQuick Check:
Use where with operator '>' = A [OK]
- Using incorrect method names like filter or query
- Using wrong operator symbols like =>
- Missing quotes around operator
db.collection('products')
.where('price', '>=', 10)
.where('price', '<=', 50)
.where('category', '==', 'books')
.orderBy('price')
.get()What will happen when this query runs?
Solution
Step 1: Analyze the filters used
The query uses two range filters on the same field 'price' (greater or equal 10 and less or equal 50) and an equality filter on 'category'.Step 2: Check Firebase query rules
Firebase allows multiple range filters on the same field combined with equality filters on other fields, and ordering by the range field is valid.Final Answer:
It will return all books priced between 10 and 50 sorted by price. -> Option AQuick Check:
Multiple range filters on same field allowed = C [OK]
- Thinking multiple range filters on same field cause errors
- Assuming equality filters can't combine with range filters
- Ignoring orderBy requirement on range field
db.collection('orders')
.where('status', '==', 'shipped')
.where('total', '>', 100)
.where('date', '>', '2023-01-01')
.get()But it throws an error. What is the likely cause?
Solution
Step 1: Identify filters used
The query has one equality filter on 'status' and two range filters on 'total' and 'date' fields.Step 2: Recall Firestore query limitations
Firestore allows only one range filter on a single field per query, so having range filters on two different fields causes an error.Final Answer:
You cannot have two range filters on different fields in one query. -> Option DQuick Check:
Only one range filter field allowed = D [OK]
- Thinking order of filters matters
- Assuming Firestore can't filter by date
- Believing collection name causes this error
status is 'active', priority is greater than 5, and createdAt is after '2023-01-01'. How can you work around Firestore's query limitations to get this data?Solution
Step 1: Understand Firestore query limits
Firestore does not allow multiple range filters on different fields in a single query.Step 2: Apply workaround using multiple queries
Run two queries: one filtering by status and priority, another by status and createdAt, then combine results in your application code.Final Answer:
Run two separate queries: one filtering by status and priority, another by status and createdAt, then merge results in code. -> Option CQuick Check:
Multiple queries merge workaround = A [OK]
- Trying to combine all filters in one query causing errors
- Assuming composite fields solve all filter issues
- Expecting Firestore to support full-text search natively
Ready to prove it?
Pass the test and this pattern moves unknown
