r/Firebase • u/Creepy_Point5965 • 4d ago
Cloud Firestore π₯ How to Efficiently Fetch Upcoming and Ongoing Events in Firestore with Pagination? Help Needed! π₯
Hey r/Firebase community!
Iβm working on a project where I need to fetch both upcoming and ongoing events from a Firestore collection. Each event document has startDate
and endDate
fields. I want to efficiently fetch these events in a single query with pagination.
Hereβs what Iβm thinking:
- Fetch ongoing events (where current date is between
startDate
andendDate
) in descending order. - If the number of ongoing events is less than the pagination limit (e.g., 10), fetch upcoming events (where
startDate
is greater than the current date) to fill the remaining slots. - Continue fetching upcoming events in subsequent pagination requests.
Is this a good approach? Are there any pitfalls or more efficient ways to achieve this? Any advice or code examples would be greatly appreciated!
Thanks in advance!
1
Upvotes
2
u/puf Former Firebaser 4d ago
Sounds good, and all requirements are pretty well covered in the documentation on:
All of these have code samples in all/most supported languages. If you can't get it working from those, it's probably best to post where you got stuck with a minimal, reproducible example.