what would be efficient alternative of "JOIN" in Firestore(NoSQL)?

0

Issue

I have users collection & transactions collection.

I need to get the user’s balance by calculating his/her transactions.

And I heard that you are allowed to make duplicates and denormalize your database to achieve less document read in one request. (reading many docs cost more)

My approaches:

  1. set transaction collection as a "subcollection" in the user document, so that you only get a user’s documentation and compute the values need on the client-side.
  2. make those collections as TOP level collections separately and somehow make "JOIN" queries to get his/her transactions then compute the value on the client-side.
  3. Just make a field named "balance" in the user’s document and update it every time they make transactions. (But this seems not quite adaptable to changes that might be made in the future)

Which approach is efficient? Or Maybe are there totally different approaches?

Solution

Which approach is efficient?

The third one.

Or Maybe are there totally different approaches?

Of course, there are, but by far the third is the best and cheapest one. Every time a new transaction is performed simply increment the "balance" field using:

Answered By – Alex Mamo

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More