且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何在集合中的文档中获取所有子集合

更新时间:2023-02-14 08:53:32

如果要从所有 Product 集合中获取文档,则可以使用

If you want to get the documents from all Product collections, you can use a collection group query.

要使用它,请执行以下操作:

To use it, you'd do:

await firebase.firestore().collectionGroup('Product').onSnapshot(sanpham => {
  sanpham.forEach((doc) => {
    a.push({ id: doc.id, data: doc.data() })
  })
})

如果您想知道产品属于哪个用户,则可以使用以下方法确定该信息:

If you want to know which user a product belongs to, you can determine that with:

const productRef = doc.ref;
const productCollectionRef = productRef.parent;
const userRef = productCollectionRef.parent;
console.log(userRef.id);