19

How do you perform cheap joins with Firestore?

In Firebase, I would .map() the response and get the additional data based on a foreign key stored on each item. However, considering the pricing model of Firestore where you pay per reads, this appears to be too expensive. What do you think?

In my case, my relationship is many actions to a handful of categories (circa 5 - 7). Each action belongs to one category.

What would be the best practice for this case? Should I keep doing it like in Firebase? Or should I fetch both collections independently and join them in Javascript?

Jakub

PS How do you actually work with the reference data type? It is unfortunately not described in the documentation.

Sampath
  • 63,341
  • 64
  • 307
  • 441
Jakub
  • 479
  • 5
  • 17
  • Have you check: https://stackoverflow.com/questions/46568850/what-is-firestore-reference-data-type-good-for ? – Wandrille Oct 14 '17 at 16:01
  • Thanks, yes, I have. Unfortunately, the comment with the most votes asks for an update of the documentation. It is not clear how to use the reference data type. – Jakub Oct 14 '17 at 16:43
  • Endeed. I'm also interested of an answer. – Wandrille Oct 14 '17 at 17:13

1 Answers1

9

Cloud Firestore, as you noted, charges for each document read. This is based on the number of documents returned to you when you make a query. It does not matter how many individual requests you make to get the documents (assuming each request returns >= 1 document). So it will be cheaper for you to do the map() method than to fetch all documents and join them in memory, since you will be reading fewer documents from the backend.

If you share more about your data model (I can't picture it in my head) there may be a way to reduce the need for joins by duplicating some data or leveraging queries.

Sam Stern
  • 24,624
  • 13
  • 93
  • 124