Abdiaziz Ahmed 3L4G
Abdiaziz Ahmed 3L4G

Reputation: 27

How should I structure Firestore documents with a large number of docs needed to be read each time

I have a list of service that each have a number of fields that are structured like this

Service1*

     ServiceID*
     NetID*
     Country*
     price*
     xxx*
     xxx*
     10 fields in total*

Service2*

     ServiceID*
     NetID*
     Country*
     price*
     xxx*
     xxx*
     10 fields in total*

I have arranged currently where each service is its own document for a total of 1,180. However when a user visits my site and wants to select a service, the server would fetch all 1180. With just 100users firebase would reading 100k docs. I was thinking of saving a copy of all the services as a csv and loading it from there every time a user wants to search the service.

Is there any better of structuring the data or using another method to reduce my read count of Firestore.

Upvotes: 0

Views: 47

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

Is there any better of structuring the data

There is no other better method for doing that.

However when a user visits my site and wants to select a service, the server would fetch all 1180.

That's not a proper way to interact with Cloud Firestore. You are downloading huge ammout of data with a high cost. Beside that, a user will never need all that amount of data at once. A solution for that will be to load data in smaller chunks. For Android, I recommend you see my answer from the following post:

I was thinking of saving a copy of all the services as a csv and loading it from there every time a user wants to search the service.

There is no need for that. Firestore already has its own caching mechanism. So once you get a document, it will always be read from cache, as long as on the server, no new changes are made. More infos here:

For the web, offline persistence is disabled by default. To enable persistence, call the enablePersistence method. Cloud Firestore's cache isn't automatically cleared between sessions. Consequently, if your web app handles sensitive information, make sure to ask the user if they're on a trusted device before enabling persistence.

Upvotes: 2

Related Questions