How to Query Firestore: Simple Syntax and Examples
To query Firestore, use the
collection() method to select a collection, then chain where() filters to specify conditions, and finish with get() to retrieve matching documents. Queries return snapshots you can loop through to access data.Syntax
Firestore queries start by selecting a collection with collection('name'). You can add filters using where('field', 'operator', value) to narrow results. Use orderBy('field') to sort, and limit(number) to restrict results. Finally, call get() to fetch the data.
Each part:
collection(): Choose the collection to query.where(): Filter documents by field conditions.orderBy(): Sort results by a field.limit(): Limit number of documents returned.get(): Execute the query and get results.
javascript
const query = firestore.collection('users') .where('age', '>=', 18) .orderBy('age') .limit(10) .get();
Example
This example queries the users collection for users aged 18 or older, orders them by age, and limits to 10 results. It then prints each user's name and age.
javascript
import { initializeApp } from 'firebase/app'; import { getFirestore, collection, query, where, orderBy, limit, getDocs } from 'firebase/firestore'; const firebaseConfig = { // your config here }; const app = initializeApp(firebaseConfig); const db = getFirestore(app); async function getAdultUsers() { const usersRef = collection(db, 'users'); const q = query(usersRef, where('age', '>=', 18), orderBy('age'), limit(10)); const querySnapshot = await getDocs(q); querySnapshot.forEach((doc) => { const data = doc.data(); console.log(`${data.name} is ${data.age} years old`); }); } getAdultUsers();
Output
Alice is 18 years old
Bob is 20 years old
Carol is 22 years old
... (up to 10 users)
Common Pitfalls
Common mistakes when querying Firestore include:
- Not creating indexes for complex queries with multiple
whereororderByclauses, causing errors. - Using inequality filters (
<,>) on multiple fields in the same query, which Firestore does not allow. - Forgetting to
awaitasynchronous calls likegetDocs(), leading to empty or unresolved results. - Not handling empty query results, which return an empty snapshot rather than an error.
javascript
/* Wrong: Using inequality on two fields */ const q = query(collection(db, 'users'), where('age', '>=', 18), where('score', '<', 50)); /* Right: Use only one inequality filter */ const q = query(collection(db, 'users'), where('age', '>=', 18), where('score', '==', 50));
Quick Reference
| Method | Purpose | Example |
|---|---|---|
| collection('name') | Select a collection | collection('users') |
| where(field, op, value) | Filter documents | where('age', '>=', 18) |
| orderBy(field) | Sort results | orderBy('age') |
| limit(number) | Limit results count | limit(10) |
| getDocs(query) | Execute query and get data | const snapshot = await getDocs(q) |
Key Takeaways
Use collection(), where(), orderBy(), and limit() to build Firestore queries.
Always await asynchronous query calls like getDocs() to get results.
Firestore requires indexes for complex queries with multiple filters or sorting.
Only one inequality filter is allowed per query on a single field.
Handle empty query results gracefully to avoid errors.