0
0
Firebasecloud~5 mins

In and not-in queries in Firebase - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to find documents in a database where a field matches one of several values or does not match certain values. In and not-in queries help you filter data easily without checking each value one by one.
When you want to find users whose status is either 'active' or 'pending' in a single query.
When you need to exclude products that belong to certain categories from your search results.
When you want to get orders that match any of a list of order IDs.
When you want to filter events that are not in a list of canceled event IDs.
When you want to quickly check if a field matches any value from a small list without multiple queries.
Commands
This command queries the 'users' collection to find documents where the 'status' field is either 'active' or 'pending'. It uses the 'in' operator to match any value in the list.
Terminal
firebase firestore:query --collection=users --where="status in ['active','pending']"
Expected OutputExpected
Document ID: user123, status: active Document ID: user456, status: pending
--collection - Specifies which collection to query
--where - Defines the filter condition for the query
This command queries the 'products' collection to find documents where the 'category' field is not any of the values in the list. It uses the 'not-in' operator to exclude those categories.
Terminal
firebase firestore:query --collection=products --where="category not-in ['electronics','furniture']"
Expected OutputExpected
Document ID: prod789, category: clothing Document ID: prod101, category: toys
--collection - Specifies which collection to query
--where - Defines the filter condition for the query
Key Concept

If you remember nothing else from this pattern, remember: 'in' finds documents matching any value in a list, while 'not-in' excludes documents matching any value in a list.

Common Mistakes
Using 'in' or 'not-in' with more than 10 values in the list
Firestore limits 'in' and 'not-in' queries to 10 values max, so the query will fail if exceeded.
Split the values into multiple queries or use other filtering methods.
Trying to combine 'in' and 'not-in' on the same field in one query
Firestore does not allow multiple inequality filters on the same field in a single query.
Run separate queries or adjust your data model.
Summary
Use 'in' queries to find documents where a field matches any value in a list.
Use 'not-in' queries to exclude documents where a field matches any value in a list.
Remember Firestore limits these queries to 10 values per list and does not allow combining them on the same field.