📖 Scenario: You are building a simple app that shows a list of books stored in a Firebase Firestore database. The list can be very long, so you want to show only a few books at a time and let users move forward or backward through the list.
🎯 Goal: Create a Firestore query that fetches books in pages of 3 items using cursor-based pagination with startAfter and endBefore methods.
📋 What You'll Learn
Create a Firestore collection reference called booksRef pointing to the 'books' collection.
Create a query to fetch the first 3 books ordered by the field title.
Add a variable lastVisible to store the last document snapshot from the current page.
Create a query to fetch the next 3 books using startAfter(lastVisible).
Create a query to fetch the previous 3 books using endBefore(firstVisible).
💡 Why This Matters
🌍 Real World
Cursor-based pagination is used in apps to efficiently load large lists of data without overwhelming the user or the database.
💼 Career
Understanding Firestore pagination is important for building scalable cloud applications that handle data smoothly.
Progress0 / 4 steps
1
Set up Firestore collection reference
Create a Firestore collection reference called booksRef that points to the 'books' collection.
Firebase
Hint
Use firestore.collection('books') to get the collection reference.
2
Create initial query for first page
Create a query called firstPageQuery that fetches the first 3 books ordered by the title field.
Firebase
Hint
Use orderBy('title') and limit(3) on booksRef.
3
Add variable to store last visible document
Create a variable called lastVisible to store the last document snapshot from the current page.
Firebase
Hint
Initialize lastVisible with null before fetching data.
4
Create queries for next and previous pages
Create two queries: nextPageQuery that fetches the next 3 books using startAfter(lastVisible), and prevPageQuery that fetches the previous 3 books using endBefore(firstVisible). Assume firstVisible is the first document snapshot of the current page.
Firebase
Hint
Use startAfter(lastVisible) for next page and endBefore(firstVisible) for previous page queries.
Practice
(1/5)
1. What does the startAfter method do in Firebase cursor-based pagination?
easy
A. It loads all documents from the beginning.
B. It ends the query before a specific document snapshot.
C. It deletes documents after a specific snapshot.
D. It starts loading documents after a specific document snapshot.
Solution
Step 1: Understand the purpose of startAfter
startAfter tells Firebase to begin loading documents after the given document snapshot, skipping that snapshot itself.
Step 2: Compare with other cursor methods
endBefore stops before a snapshot, but startAfter moves forward after it.
Final Answer:
It starts loading documents after a specific document snapshot. -> Option D
Quick Check:
startAfter = start after snapshot [OK]
Hint: Remember: startAfter skips the given document and moves forward [OK]
Common Mistakes:
Confusing startAfter with endBefore
Thinking startAfter includes the given document
Assuming startAfter loads all documents from start
2. Which of the following is the correct syntax to start a query after a document snapshot named lastDoc in Firebase?
easy
A. query.startAfter(lastDoc)
B. query.endAfter(lastDoc)
C. query.startBefore(lastDoc)
D. query.endBefore(lastDoc)
Solution
Step 1: Recall Firebase query cursor methods
The correct method to start after a document is startAfter().
Step 2: Match method with snapshot variable
Using startAfter(lastDoc) correctly applies the cursor after the lastDoc snapshot.
Final Answer:
query.startAfter(lastDoc) -> Option A
Quick Check:
Correct method is startAfter() [OK]
Hint: Use startAfter(snapshot) to move forward in pagination [OK]
Common Mistakes:
Using non-existent methods like endAfter or startBefore
Mixing up startAfter and endBefore
Forgetting to pass the snapshot as argument
3. Given the following code snippet, what documents will be returned?
A. Using limitToLast without endBefore causes error
B. You must use startAfter instead of endBefore
C. The lastDoc snapshot is not from the same query order
D. Firebase does not support endBefore with limitToLast
Solution
Step 1: Check cursor snapshot origin
The snapshot lastDoc must come from a query with the same ordering and filters.
Step 2: Understand error cause
If lastDoc is from a different query or unordered, Firebase throws an error.
Final Answer:
The lastDoc snapshot is not from the same query order -> Option C
Quick Check:
Cursor snapshot must match query order [OK]
Hint: Cursor snapshot must come from same ordered query [OK]
Common Mistakes:
Using snapshot from different query or order
Mixing startAfter and endBefore incorrectly
Ignoring need for consistent ordering
5. You want to implement backward pagination to load previous 5 documents before a known document snapshot currentDoc. Which Firebase query correctly achieves this?
hard
A. collection.orderBy('timestamp').startAfter(currentDoc).limit(5)
B. collection.orderBy('timestamp').endBefore(currentDoc).limitToLast(5)
C. collection.orderBy('timestamp').startAt(currentDoc).limit(5)
D. collection.orderBy('timestamp').endAt(currentDoc).limitToLast(5)
Solution
Step 1: Understand backward pagination
To load documents before currentDoc, use endBefore(currentDoc).
Step 2: Use limitToLast for correct order
Using limitToLast(5) returns the last 5 documents before currentDoc in correct order.
Final Answer:
collection.orderBy('timestamp').endBefore(currentDoc).limitToLast(5) -> Option B