1

I am making an app on asking questions and getting answers when reading books, using firestore collections: books and users. For the each book document, there is a questions subcollection: books/isbn/questions/{questionid}. For each questionid which is auto generated, there are attributes including asker_uid which is the user id of the user who asked the question.

Now, for one particular user, if I want to list all the questions he asked. I can do group collection query:

 firebase.firestore().collectionGroup('questions').where('asker_uid', '==', myUser.uid).get()
        .then((snapshot) =>

Then, if in the list of questions I want to show the details of the books for which the questions are asked, for each question doc in the snapshot, I can find the isbn by the property: doc.ref.parent.parent.id . After finding the isbn, then I can do another query to show the title and author etc for that book, which are attributes for that isbn.

firebase.firestore().collection('books').doc(doc.ref.parent.parent.id).get().then(
           (bookitem) =>

My question is, whether it is better to put all the information of the book directly in the question? (The method seems to be called denormalization, https://github.com/firebase/firebase-js-sdk/issues/1798) This way, after group collection query, I can directly get all info (e.g., book title) I need for each snapshot of question. The con side is that I need put extra data for each question in one book. And there can be many questions in one book.

The other approach is as I described with the code block above, for each question I trace back to the grandparent id and then do another query to get the info of the book. The pro is saving of storage and it feels clean. The con is there are additional queries, and at the current moment I am still stuck on how to do the nesting of queries (first collection group query, then for each snapshot do another query for the isbn) in a useEffect hook in my react native code (the book title property can be shown with console.log inside the code block of the second query, but becomes undefined outside it).

In general, I think this is somewhat relevant to these previous questions:

Get parents from Firestore Collection Group Query, WITHOUT RETRIEVING SONS

Firestore - get the parent document of a subcollection

Would appreciate your opinion/suggestion!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
letmeask
  • 43
  • 7

1 Answers1

0

I think the approach to Denomolize your data is better. Since

  • It will make your data structure scalable, you will face more cases like this if you structure your data by nested
Thanh Le
  • 56
  • 4
  • Thanks for your opinion! I would like to know if anyone had experience with making this kind of decision. While the denormalization seems easier, it leads to a lot duplicated data, and I don't know whether there is a tool/procedure in firestore to make sure the synchronization of the duplicated data in case changes need to be made. – letmeask Apr 09 '21 at 15:07
  • The approach to denormalization is something to reduce the duplicated data. That is how relation SQL use to reduce duplicate. Do you have any cases that denormalize that will make data duplicated – Thanh Le Apr 12 '21 at 02:11
  • You probably got the denormalization concept wrong! Pls check https://en.wikipedia.org/wiki/Denormalization and the link I mentioned in the post https://github.com/firebase/firebase-js-sdk/issues/1798 – letmeask Apr 12 '21 at 20:23