-1

I have an SQL background and am trying to get my head around document storage. For a Firebase database structure as follows:

  • John (doc)
    • Restaurant Reviews (collection)
      • Review 1 (doc)
      • Review 2 (doc)
  • Paul (doc)
    • Restaurant Reviews (collection)
      • Review 3 (doc)
      • Review 4 (doc)

I'm using a CollectionGroup to get all reviews for a specific restaurant (the restaurant Id is on the review document). This works well. I also want to get some metadata about the user. I can achieve this with a secondary query when iterating over each review that was returned:

restaurantReviewsSnapshot.forEach(reviewSnap => {
   reviewSnap.ref.parent.parent.get().then(reviewerSnap => {
   ...

However, this is not efficient. Is it possible to get this related data in some sort of a join operation in one go?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Jonny
  • 394
  • 7
  • 17

1 Answers1

3

In your case, one classical approach in the NoSQL world is to denormalize the data in such a way your query is easy to build and execute.

More concretely it means that in each Review doc you would duplicate the data of its author.

One side effect of this approach is that you need to keep the values in sync (i.e. the one in the user/Author document and the ones in the Review documents). This synchronization could be done with a Cloud Function triggered if the Author doc is modified.


Again, you should not be afraid to duplicate data and denormalize your data model. Here is a "famous" post about NoSQL data-modeling approaches: https://highlyscalable.wordpress.com/2012/03/01/nosql-data-modeling-techniques/

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121