Google Professional Cloud Developer Question 455
Select 3Google Cloud PlatformYou are building an API using Google Cloud Functions to retrieve a list of user records from a Firestore database. The database contains thousands of records, and you need to implement pagination to return results in chunks of 50 items per request. Which of the following approaches would meet this requirement?
- A
Use Firestore's
limit()method to fetch 50 records and use the last document's ID as a starting point for the next query. - B
Retrieve all records from Firestore and implement pagination on the client side by slicing the results.
- C
Use Firestore's
startAt()orstartAfter()methods along withlimit()to fetch 50 records per request. - D
Store the entire dataset in Google Cloud Storage as a JSON file and implement pagination on the client side.
- E
Use Firestore's
orderBy()method in combination withstartAfter()andlimit()for efficient pagination.
Show answer and explanation
Correct answers: A, C, E
Explanation
Efficient pagination in Firestore involves using query methods like limit() to fetch a fixed number of records and startAt() or startAfter() to define the starting point for subsequent queries. This approach minimizes resource usage and ensures scalability. Combining these methods with orderBy() is recommended for consistent results, especially when dealing with large datasets.
- A. Correct.
Correct. Using
limit()fetches a fixed number of records, and using the last document's ID as a starting point for the next query ensures consistent pagination. - B. Incorrect.
Incorrect. Fetching all records and slicing them on the client side is inefficient, especially for large datasets, as it increases memory usage and network overhead.
- C. Correct.
Correct. The
startAt()orstartAfter()methods allow you to define a starting point for the next query, and combining this withlimit()enables proper pagination. - D. Incorrect.
Incorrect. Storing the dataset in Google Cloud Storage and loading it on the client side is not a scalable or efficient approach for real-time querying.
- E. Correct.
Correct. Using
orderBy()ensures the results are sorted, and combining it withstartAfter()andlimit()provides efficient and reliable pagination.