Comparison operators (==, <, >, >=, <=) in Firebase - Time & Space Complexity
When using comparison operators in Firebase queries, it's important to know how the number of comparisons grows as data increases.
We want to understand how many comparisons Firebase makes when filtering data with operators like ==, <, >, >=, and <=.
Analyze the time complexity of querying documents with a comparison filter.
const query = firestore.collection('users')
.where('age', '>=', 18)
.where('age', '<', 30)
.get();
query.then(snapshot => {
snapshot.forEach(doc => {
console.log(doc.data());
});
});
This code fetches users whose age is between 18 and 29 inclusive, using comparison operators in the query.
Let's find the main repeated actions in this query.
- Primary operation: Firebase performs comparisons on the 'age' field for each document in the collection or index.
- How many times: Once per document scanned or index entry checked.
As the number of documents grows, Firebase must check more entries to find matches.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | About 10 comparisons |
| 100 | About 100 comparisons |
| 1000 | About 1000 comparisons |
Pattern observation: The number of comparisons grows roughly in direct proportion to the number of documents scanned.
Time Complexity: O(n)
This means the time to complete the query grows linearly with the number of documents checked.
[X] Wrong: "Comparison queries always run instantly no matter how much data there is."
[OK] Correct: Firebase still needs to check documents or index entries to apply comparisons, so more data means more work.
Understanding how comparison operations scale helps you design efficient queries and shows you think about real-world data growth.
"What if we added an index on the 'age' field? How would that change the time complexity of these comparison queries?"
Practice
Solution
Step 1: Understand the purpose of each operator
The operator==checks for exact equality, while<,>=, and<=check for less than, greater or equal, and less or equal respectively.Step 2: Identify the operator for exact equality
Since the question asks for exact equality, the correct operator is==.Final Answer:
== -> Option AQuick Check:
Exact equality operator = == [OK]
- Confusing == with assignment operator =
- Using < or > for equality checks
- Thinking >= means equal only
age is greater than 18?Solution
Step 1: Identify the operator for 'greater than'
The operator for 'greater than' is>.Step 2: Match the operator with the correct syntax
The correct syntax usesquery.where('age', '>', 18)to filter documents where age is greater than 18.Final Answer:
query.where('age', '>', 18) -> Option DQuick Check:
Greater than operator = > [OK]
- Using '==' instead of '>'
- Confusing '>=' with '>'
- Using '<' when wanting greater values
query.where('score', '>=', 50).where('score', '<', 70), which scores will be included in the results?Solution
Step 1: Analyze the first condition
The first conditionscore >= 50includes scores 50 and above.Step 2: Analyze the second condition
The second conditionscore < 70includes scores less than 70, excluding 70 itself.Step 3: Combine both conditions
Only scores that satisfy both conditions are included: scores greater than or equal to 50 and less than 70.Final Answer:
Scores greater than or equal to 50 and less than 70 -> Option AQuick Check:
Range includes 50 ≤ score < 70 [OK]
- Including 70 when using '<' operator
- Confusing '>' with '>='
- Thinking 'or' instead of 'and' between conditions
query.where('price', '=>', 100). Why does it cause an error?Solution
Step 1: Check the operator syntax
The operator'=>'is not a valid Firebase comparison operator. The correct operator for 'greater than or equal' is'>='.Step 2: Understand Firebase operator rules
Firebase only supports specific operators:==,<,>,<=, and>=. Using any other operator causes a syntax error.Final Answer:
'=>' is invalid operator causing error -> Option CQuick Check:
Use >=, not => for greater or equal [OK]
- Typing => instead of >=
- Using strings instead of numbers for numeric fields
- Assuming all operators are valid
temperature is between 20 and 30 inclusive. Which query correctly uses comparison operators to achieve this?Solution
Step 1: Understand inclusive range requirements
Inclusive means including both 20 and 30 values.Step 2: Choose operators that include boundary values
>=includes 20 and<=includes 30, so these operators match the requirement.Step 3: Verify the combined query
The queryquery.where('temperature', '>=', 20).where('temperature', '<=', 30)correctly filters temperatures from 20 to 30 inclusive.Final Answer:
query.where('temperature', '>=', 20).where('temperature', '<=', 30) -> Option BQuick Check:
Inclusive range uses >= and <= [OK]
- Using > and < excludes boundary values
- Using == twice filters only exact matches
- Reversing operators causing empty results
Ready to prove it?
Pass the test and this pattern moves unknown
