这是我的结构:
javascript - 在子集合中查询?-LMLPHP
javascript - 在子集合中查询?-LMLPHP

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

我这样查询用户:

 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
        });
      });
  }


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

最佳答案

在您的代码中,snapDocumentSnapshot类型的对象,其中包含对在其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

08-03 23:06
查看更多