0

Data in Firestore

So here I Have multiple sub-collections(subjects) in different doc's(grades) and I want to get all the sub-collections(subjects) documents(questions) at once I tried to get them by using Collection group queries the only problem which I am facing in my code sometime it returning all the doc's(questions) but sometimes not what is the issue

this is what i have tried

 const getAllQuestions = (request,response)=>{
    const subjects = ['Maths','English']
    const questionsArray = []
    subjects.forEach((subject,index)=>{
        
     db.collectionGroup(subject)
                       .get()
                       .then((querySnapshot)=>{
                        querySnapshot.forEach((doc) => {
                            questionsArray.push({...doc.data(),id:doc.id})
                        })
                        if(index==subjects.length-1){
                            response.status(200).json({
                                    status:200,
                                    data:questionsArray,
                                    length:questionsArray.length
                          })
                         }
      })
    })
 }
  • "sometime it returning all the doc's(questions) but sometimes not" Without more information on what is different between the times it works and the times it doesn't it will be hard to help. If the not working cases are reproducible, I recommend doing some local debugging: set a breakpoint on each line of the code you shared, run in a debugger, and check the value of each variable on each line. What is the *first* line that doesn't do what you expect it to do? – Frank van Puffelen Nov 12 '22 at 15:08
  • @FrankvanPuffelen can you please tell me what I should do if I have the same sub-collection (with the same names Ex - Maths, English) in any other top-level collections which I am not want to get otherwise collection group query will be fetched them as well isn't it? – Ritik Joshi Nov 14 '22 at 09:48

1 Answers1

0

If you don't want to get the subcollections from all grades, but only from one of them, you should not use a collection group query but instead specify the entire path to the collection you want to query/read:

 db.collection('quizQuesDb/Grade 5/'+subject)
                   .get()

If you want to perform a query across all collections of a certain name under a specific path, see: CollectionGroupQuery but limit search to subcollections under a particular document

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • yes you are right, but I want to get all sub-collection and the problem is if let's say I have the same name sub-collection anywhere in my database(which contains different context data) it will also be fetched in my query which I do not want – Ritik Joshi Nov 14 '22 at 16:10
  • So, did you try the code I gave in my answer? Did that work? If not, what was the problem? – Frank van Puffelen Nov 14 '22 at 16:44
  • yes it is working but it will give me only one sub-collection doc and I want to get all sub-collection docs now I am also able to get all sub-collection docs as well by using `db.collectionGroup(sub-collection-name)` but the problem is this collectionGroup query is applied on the entire database but I want to use this collectionGroup query only for those sub-collections which are inside a **particular** collection not anywhere in db, so can we use collectionGroup against sub-collection inside a particular collection? – Ritik Joshi Nov 15 '22 at 17:33
  • See https://stackoverflow.com/questions/68049541/collectiongroupquery-but-limit-search-to-subcollections-under-a-particular-docum/68049847#68049847 – Frank van Puffelen Nov 15 '22 at 20:50