[英]Query through all documents in a collection, and through all subcollections in firestore
[英]Firestore - How retrieve all subcollections of collection
我有一个关于firestore的问题,我创建了一个“类别”集合,其中有几个文档,我创建了一个“建筑工地”子集合,我想从每个类别中检索我所有的子 collections:但是当我尝试做像这样:
useEffect(() => {
const listConstruction = [];
db.collection('category').get().then((collectionCategory) => {
collectionCategory.forEach((doc) => {
doc.ref.collection('construction').get().then((collectionConstruction) => {
collectionConstruction.forEach((doc1) => {
listConstruction.push({
idCategory: doc.id,
libelleCategory: doc.data().libelle,
title: doc1.data().title,
description: doc1.data().description,
});
});
});
});
}).finally(() => {
setConstruction(listConstruction);
});
}, []);
问题是它还没有完成将它传递给我的构造最终!
还没有使用 NOSQL 和 Firestore 的经验,如果我对它的使用没有完全错误,我会希望如何解决?
谢谢
如果我正确理解您的问题,您需要使用Collection Group query 。
如文档中所述:
一个集合组由所有具有相同 ID 的 collections 组成。 默认情况下,查询从数据库中的单个集合中检索结果。 使用集合组查询从集合组而不是单个集合中检索文档。
旁注:如果你想在forEach
循环中调用异步方法,你应该使用Promise.all()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.