Google Professional Cloud Developer Question 454
Single answerGoogle Cloud PlatformYou are developing an API in Google Cloud that retrieves a potentially large dataset from a Firestore collection. To ensure the API can handle large datasets efficiently while retrieving results in manageable chunks for the client, you decide to implement pagination. Which approach should you use to paginate the Firestore query results?
- A
Use Firestore’s startAt() and limit() methods to define the starting point and the number of results to return.
- B
Use Firestore’s batchGet() method to retrieve all documents at once and implement pagination on the client side.
- C
Use Firestore’s orderBy() method combined with limit() to retrieve results sequentially.
- D
Use Firestore’s startAfter() method paired with limit() to continue retrieving results after the last retrieved document.
Show answer and explanation
Correct answer: D
Explanation
Firestore provides built-in methods to handle pagination efficiently. The startAfter() method is specifically designed to resume queries after the last retrieved document when used with limit(). This allows you to paginate through large datasets without fetching all data at once, ensuring efficient memory and performance management.
- A. Incorrect.
This method is partially correct but not sufficient for proper pagination. While startAt() allows you to define a starting point, it does not allow you to resume efficiently from the last retrieved document in subsequent queries.
- B. Incorrect.
This approach fetches all documents at once, which is inefficient for large datasets and does not leverage Firestore’s built-in pagination capabilities. It can lead to memory and performance issues.
- C. Incorrect.
While orderBy() is necessary to define the sorting for the query, it does not suffice for pagination on its own. Pagination requires using startAfter() or startAt() in combination with limit().
- D. Correct.
This is the correct approach. Using startAfter() with limit() allows you to fetch a specific number of results after the last retrieved document, enabling efficient pagination of Firestore results.