简体   繁体   English

如何从 firestore 一次获取多个集合组的文档?

[英]How to get multiple collection group's documents at once from firestore?

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
                          })
                         }
      })
    })
 }

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如果要对特定路径下某个名称的所有collections进行查询,请参见: CollectionGroupQuery但限制搜索特定文档下的子集合

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何一次将多个文档添加到 Firestore 中的一个集合中? - How do you add multiple documents at once to a collection in Firestore? 无法从 FIRESTORE 的集合中获取多个文档 - UNABLE TO Get multiple documents from a collection FROM FIRESTORE 我想从 firestore 中的集合中获取多个字段的文档 - I want to get documents for multiple fields from a collection in firestore 如何从 Firestore 的集合中获取所有文档 - How to get all documents from a collection from Firestore 从 Firestore 集合中获取包含所有文档的列表 - Get a list with all documents from a Firestore collection Firestore:如何获取集合中的随机文档 - Firestore: How to get random documents in a collection 如何根据 Cloud Firestore 中的主集合字段和子集合字段查询子集合中的文档? - How to query documents from a sub-collection, based on a main collection's field and a sub-collection's fields in cloud firestore? 我如何知道是否还有更多文档需要从 Firestore 集合中获取? - How do I know if there are more documents left to get from a firestore collection? Firestore 从根集合中获取所有文档(包含子集合) - Firestore get all documents (contain subcollections) from a root collection Firestore 从集合中文档的 get() 中排除特定字段 - Firestore exclude specific fields from get() of documents in collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM