1

Assume I have an application that shows a list of restaurants & 1000 restaurants to show.

My first impression would be to create a collection of restaurants and each individual restaurant would be a document inside this collection.

The concern with the above approach is that for each user Cloud Firestore would register 1000 reads.

My question is if there is a better way of storing the restaurants to decrease the number of reads?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Mathiew Abbas
  • 823
  • 1
  • 8
  • 17

1 Answers1

3

My first impression would be to create a collection of restaurants and each individual restaurant would be a document inside this collection.

Yes, that's the right way to do it.

The concern with the above approach is that for each user firestore would register 1000 reads

You'll be charged with 1000 read operations only if you read all documents at once. But this is not the right way to do it, you need to limit the data that you get. On how you can achieve this, please check the official documentation regarding order and limit data in Cloud Firestore.

Another most apropiate approach is to load the data in smaller chunks. This practice is called pagination and can be used very simply in Cloud Firestore using startAt() or startAfter() methods.

For Android, this is a recommended way in which you can paginate queries by combining query cursors with the limit() method. I also recommend you take a look at this video for a better understanding.

My question is if there is a better way of storing the restaurants to decrease the number of reads?

And to answer your question, the problem is not about how you store the data is about how you read it.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193