如何在 Firebase Firestore 中查找文档的父集合?

Bla*_*zor 4 javascript node.js firebase google-cloud-firestore

我正在 Firebase 中搜索子查询,并尝试找到它所属的父集合(然后是父文档)。这是我的代码

let queryRef = orgRef.where('User_ID', '==', userID).get()
    .then(snapshot => {
        if (snapshot.empty) {
          console.log('No matching documents.');
          return;
        }  



        snapshot.forEach(doc => {
          console.log(doc.parent());
        });
      })
      .catch(err => {
        console.log('Error getting documents', err);
      });
Run Code Online (Sandbox Code Playgroud)

但是使用 doc.parent 或 snapshot.parent 不起作用。如何找到子集合的父文档?

Dou*_*son 6

如果您有一个 DocumentSnapshot 对象,您将需要使用其ref属性来获取指向它的DocumentReference对象。DocumentReference 有一个parent属性,可以让您获取文档所在的集合。所以,您可能会想要:

doc.ref.parent
Run Code Online (Sandbox Code Playgroud)

对于任何给定的doc查询结果。