[英]How to get multiple collection group's documents at once from 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.