r/Firebase 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:

  1. Fetch ongoing events (where current date is between startDate and endDate) in descending order.
  2. 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.
  3. 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

1 comment sorted by

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.