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

谁都会走 提交于 2020-01-11 13:46:05

问题


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.


回答1:


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:

  • How to paginate Firestore with Android?

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:

  • https://firebase.google.com/docs/firestore/manage-data/enable-offline

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.



来源:https://stackoverflow.com/questions/58589798/how-should-i-structure-firestore-documents-with-a-large-number-of-docs-needed-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!