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

编程入门 行业动态 更新时间:2024-10-09 03:29:45
本文介绍了我如何在集合中的文档中获取所有子集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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 this collection(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);

更多推荐

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

本文发布于:2023-11-30 07:46:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649229.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文档   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!