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

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

本文介绍了我如何在集合中的文档中获取所有子集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有关此问题的文档,但看起来相当多

i looking for document talk about this my problem but it look like quite quite a few

我有一个这样的收藏collection(user1)=> uid => document(collection(Product => uid),nameUser .... bla bla)

i have one collection like thiscollection(user1)=>uid=>document( collection(Product => uid ),nameUser.... bla bla)

我让所有产品的文档用户1都非常容易,但是我如何与用户2,用户3一起获取并聚集到所有用户的阵列产品中?

i get all Product's document user1 this is quite easy but how i get this with user 2, user 3 and gathered into array product of all user?

我在下面尝试过此代码,但不理解

i have tried this code in below but it doesn't understand

getData = async () => {
    const a = [];
    await firebase.firestore().collection('user').doc().collection('Product').onSnapshot(sanpham => {

      sanpham.forEach((doc) => {
        a.push({ id: doc.id, data: doc.data() })
      })
    })
    this.setState({ data: a });
  }

推荐答案

如果要从所有 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);

这篇关于我如何在集合中的文档中获取所有子集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 07:41