skip to Main Content

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

2

Answers


  1. 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
    Login or Signup to reply.
  2. 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:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search