0
0
Firebasecloud~5 mins

Querying with where clause in Firebase - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to find only certain items in your database that match a rule. The where clause helps you pick just those items by checking if they meet a condition.
When you want to find all users who live in a specific city.
When you need to get all orders with a total price above a certain amount.
When you want to list all products that are currently in stock.
When you want to filter messages sent after a certain date.
When you want to show only tasks that are marked as completed.
Commands
This command queries the 'users' collection and returns only documents where the 'city' field equals 'New York'.
Terminal
firebase firestore:query --collection=users --where="city=='New York'"
Expected OutputExpected
[ { "name": "Alice", "city": "New York", "age": 30 }, { "name": "Bob", "city": "New York", "age": 25 } ]
--collection - Specifies which collection to search in.
--where - Defines the condition to filter documents.
This command finds all documents in the 'orders' collection where the 'totalPrice' field is greater than 100.
Terminal
firebase firestore:query --collection=orders --where="totalPrice>100"
Expected OutputExpected
[ { "orderId": "123", "totalPrice": 150, "status": "shipped" }, { "orderId": "124", "totalPrice": 200, "status": "processing" } ]
--collection - Specifies which collection to search in.
--where - Defines the condition to filter documents.
Key Concept

If you remember nothing else from this pattern, remember: the where clause lets you pick only the data that matches your rule, so you don't get everything.

Common Mistakes
Using incorrect syntax in the where clause like missing quotes or wrong operators.
The query will fail to run or return no results because the condition is not understood.
Always use the correct syntax with quotes around the condition and supported operators like ==, >, <, >=, <=.
Trying to filter on a field that does not exist in the documents.
No documents will match, so the query returns empty results.
Make sure the field you filter on exists in your documents.
Summary
Use the where clause to filter documents in a collection by a condition.
Run queries with the --collection and --where flags to get matching data.
Check your syntax carefully to avoid query errors.