在子集合中查询?

这是我的结构:

在子集合中查询?

在子集合中查询?

我想查询并显示特定用户所在的所有群组。

我这样查询用户:

 componentDidmount() {
    const uid = auth().currentUser.uid;
    firestore()
      .collectionGroup('Members')
      .where('uid','==',`${uid}`)
      .get()
      .then(doc => {
        doc.forEach(snap => {
          console.log(snap.id); // Gives User Document under Members sub-collection
        });
      });
  }

但是如何检索用户所在的文档(从“ Groups”父集合中)的groupdId?

alv123456 回答:在子集合中查询?

在您的代码中,snap是一个DocumentSnapshot类型的对象,它包含对在其ref属性中查询的文档的完整路径的引用。您可以使用此DocumentReference对象通过使用其parent属性或通过解析其path字符串沿路径查找父集合和文档。

// a CollectionReference to Groups/{groupId}/Members
snap.ref.parent

// a DocumentReference to Groups/{groupId}
snap.ref.parent.parent  

// the string ID of the above DocumentReference {groupId}
snap.ref.parent.parent.id
本文链接:https://www.f2er.com/3133621.html

大家都在问