1

I have these requirements:

Collections in Firestore:

  1. Users (id, name, email)
  2. 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:

  1. transaction 1 : {Andy, 100, pending},
  2. transaction 2 : {Budi, 200, ok},

.........

  1. 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

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Kevin Tanudjaja
  • 866
  • 1
  • 9
  • 16
  • 1
    1. If you query for the last 1000 transactions, you're billed for 1000 reads not 1 read. ([docs](https://firebase.google.com/docs/firestore/pricing#pricing_overview)) 2. Firestore is a NoSQL database, so you should just store the user's name on the transaction document if you want to access it like that frequently. – grimsteel Mar 18 '23 at 06:05

2 Answers2

0

What is the best way to query for transactions and also the related name from user on the firestore?

There is no best way it depends on your use case, You can watch this Get to know Cloud Firestore series.

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 call instead of 1 read call right?

If you get 1000 different user data for each transaction and that's 2000 read,

Is there any efficient way without wasting 1000 of my firebase free read count every time I open admin panel?

There is two way can reduce read charges:

  1. Pagination How Do I Paginate My Data?
  2. Enable offline persistence can reduce duplicated data reads see Questions about firestore cache and reads charge
flutroid
  • 1,166
  • 8
  • 24
0

What is the best way to query for transactions and also the related name from the user on the Firestore?

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.

Is there any efficient way without wasting 1.000 of my firebase free read count every time I open the admin panel? In SQL this should be the equivalent of JOIN.

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:

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