You have a Firestore collection of documents ordered by a timestamp field. You want to fetch the next page of 5 documents after a specific document snapshot using startAfter. What will be the result of the query?
const query = firestore.collection('posts').orderBy('createdAt').startAfter(lastDoc).limit(5); const snapshot = await query.get(); const results = snapshot.docs.map(doc => doc.data());
Think about how startAfter works in Firestore pagination.
startAfter excludes the document passed as cursor and returns documents strictly after it, so the page starts after lastDoc.
You want to fetch the previous page of 5 documents before a specific document snapshot using endBefore. What does the query return?
const query = firestore.collection('posts').orderBy('createdAt').endBefore(firstDoc).limitToLast(5); const snapshot = await query.get(); const results = snapshot.docs.map(doc => doc.data());
Remember how endBefore works with limitToLast in Firestore.
endBefore excludes the document passed as cursor and returns documents strictly before it. Using limitToLast fetches the last N documents before that cursor.
You have a Firestore collection with millions of documents. You want to implement cursor-based pagination for a mobile app to load data efficiently. Which approach is best to minimize read costs and latency?
Think about Firestore indexing and how cursors work for efficient queries.
Using orderBy on an indexed field with startAfter and document snapshots as cursors allows Firestore to efficiently fetch the next page without scanning all documents.
You want to ensure users can only paginate through documents they have access to in Firestore. Which security rule approach correctly enforces this during cursor-based pagination?
Consider how Firestore security rules apply to queries and documents.
Firestore security rules are evaluated per document. To secure pagination, rules must check permissions on each document returned, regardless of cursors.
While paginating through a Firestore collection using startAfter, some documents are added or deleted concurrently. What is the best practice to ensure consistent pagination results?
Think about how ordering fields affect pagination stability.
Using a stable, immutable field for ordering and cursors helps maintain consistent pagination even if documents are added or deleted concurrently.