简体   繁体   English

如何过滤子集合的文档(firebase)?

[英]How to filter in sub collection's documents (firebase)?

My problem is that I use wrong query to get the date.我的问题是我使用错误的查询来获取日期。

          const SaveDateBase = async ( e) => {
            e.preventDefault()
           

                await setDoc(doc(db, "Users", "Pompy", "Pompy", user.uid), {
                  displayName: user.displayName,
                  uid:  user?.uid,
                  modulyPV}).then(()=>{
                 console.log("moduly", modulyPV)
                  })
        
         
        
          };
        

              useEffect(() => {
                const getUsers = async (users) => {
                  const URC = query(collection(db, "Users").document("Pompy").collection("Pompy"), where("uid", "==", user?.uid));

                
        const data = await getDocs(URC)
        setModulyPV(data.docs.map((doc) => ({...doc.data(), id: doc.id})))
                }
                getUsers();
              },[])

The date are saved in date base, and I can successfully update/delete them, but I do something wrong to fetch (read?) them.日期保存在日期库中,我可以成功更新/删除它们,但是我在获取(读取?)它们时做错了。 I guess is problem with the code.我猜是代码有问题。

You can get the data in diff ways, first "Pompy" seems to be your document where you are storing a nested collection then you document "Pompy" So for retrieve that specific document should be something like:您可以通过不同的方式获取数据,首先“Pompy”似乎是您存储嵌套集合的文档,然后您记录了“Pompy”因此检索该特定文档应该类似于:

let snapshot = await db
.collection('Users')
.doc('Pompy')
.collection('Pompy')
.get()

snapshot.forEach(doc =>{
  console.log('data:', doc.data())
})

Then to query into the nested collection would be something like querying the nested collections.然后查询嵌套集合就像查询嵌套的 collections。

https://cloud.google.com/firestore/docs/samples/firestore-data-get-sub-collections?hl=es-419#firestore_data_get_sub_collections-nodejs https://cloud.google.com/firestore/docs/samples/firestore-data-get-sub-collections?hl=es-419#firestore_data_get_sub_collections-nodejs

You can also use collection groups.您还可以使用集合组。

https://firebase.google.com/docs/firestore/query-data/queries#collection-group-query https://firebase.google.com/docs/firestore/query-data/queries#collection-group-query

const pompys = query(collectionGroup(db, 'Pompy'), where("uid", "==", user?.uid));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据 Cloud Firestore 中的主集合字段和子集合字段查询子集合中的文档? - How to query documents from a sub-collection, based on a main collection's field and a sub-collection's fields in cloud firestore? 如何在 Firestore 的子集合中查询文档? - How to Query for documents in a sub collection in Firestore? 如何在flutter更新firebase的收款文件? - How to update collection documents in firebase in flutter? 获取Collections及子文档集 - Get Collections and sub collection of documents 如何在 reactJs 中更新 V9 firebase 上子集合的所有字段 - how to update sub collection's all fields on V9 firebase in reactJs 如何从 firebase 中的集合中的所有文档中获取特定数据? - How to get specific data from all documents from a collection in firebase? 如何检索 Firebase 中集合中的所有文档并添加到列表中? - How to retrieve all documents in a collection in Firebase and add to a list? 有没有办法将从子集合 Firebase 中检索到的文档存储到一个数组中以显示其信息? AngularFirestore - Is there a way to store the retrieved documents from sub-collection Firebase into an array for displaying its info? AngularFirestore 如何获取 Firebase 集合中具有特定值的所有文档? - How to get all documents in Firebase collection with specific value? 如何通过属性值查询 firebase 个文档的集合? - How do I query a collection of firebase documents by a properties value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM