且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Firestore获取子集合父文档ID-JavaScript

更新时间:2023-02-14 09:28:26

您可以使用以下方法之一:

You could use one of the following approaches:

  const query = ......;
  query
    .then(function(querySnapshot) {
      querySnapshot.forEach(function(doc) {
        console.log(doc.ref.path);
        console.log(doc.ref.parent.parent.id);
      });
    })


在每个 QueryDocumentSnapshot 上,您可以使用 path 属性,它将返回完整路径,例如 UserList1/User1/Notes/Doc1 .


On each QueryDocumentSnapshot, you can use the ref property, which returns a DocumentReference. Then, on this DocumentReference you use the path property, which will return the full path, e.g. UserList1/User1/Notes/Doc1.

或者您使用 parent DocumentReference 的> 属性,该属性返回 parent 属性(这次是 CollectionReference 的属性),以获取父 DocumentReference ,然后此父 DocumentReference id 属性.

Or you use the parent property of the DocumentReference, which returns a CollectionReference, then you use again the parent property (of the CollectionReference this time) to get the parent DocumentReference and then the id property of this parent DocumentReference.