I have these requirements:
Collections in Firestore:
- Users (id, name, email)
- Transactions (id, userId, value, status)
Supposedly I wanted to show the last 1000 transactions in my admin panel, every time I open the admin dashboard:
- transaction 1 : {Andy, 100, pending},
- transaction 2 : {Budi, 200, ok},
………
- transaction 1000 : {Charlie, 300, pending},
What is the best way to query for transactions and also the related name from the user on the Firestore?
One way is, I could read all transactions, and then for each transaction, I get the doc value from user collection, but that could result in 1001 read calls instead of 1 read call right?
Is there any efficient way without wasting 1000 of my firebase free read count every time I open the admin panel?
In SQL this should be the equivalent of JOIN
2
Answers
There is no best way it depends on your use case, You can watch this Get to know Cloud Firestore series.
If you get 1000 different user data for each transaction and that’s 2000 read,
There is two way can reduce read charges:
When you create a query against a Firestore collection to get all transactions, that would result in 1.000 read operations. If you perform an additional request for each transaction to get user data, then you’ll have other 1.000 read operations. So in total, you’ll have to pay 2.000 reads.
In the NoSQL world, there are no joins. So the best option that you have is to save user data along with the transaction. In this way, there will be no need to perform an additional request, as the data is already there. This also means that you’ll save 1.000 reads.
I also recommend you take a look at:
And: