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!