Firebase Firestore获取子集合父文档ID-JavaScript
我的Firestore数据库中有以下结构:几个Firebase Firestore获取子集合父文档ID-JavaScript,firebase,google-cloud-firestore,Firebase,Google Cloud Firestore,我的Firestore数据库中有以下结构:几个用户列表集合,其中包含用户。每个用户都可以有一个子集合Notes 我正在执行一个运行良好的子集合组查询,从整个notes子集合返回注释 我的问题是,从Notes子集合检索的Notes文档中,我可以获取父文档ID吗?这将是下面示例中的用户1。使用JavaScript。 Collection: UserList 1 Doc: User 1 Subcollection: Notes Doc: Note 1 Collection:
用户列表
集合,其中包含用户
。每个用户都可以有一个子集合Notes
我正在执行一个运行良好的子集合组查询,从整个notes子集合返回注释
我的问题是,从Notes子集合检索的Notes文档中,我可以获取父文档ID吗?这将是下面示例中的用户1。使用JavaScript。
Collection: UserList 1
Doc: User 1
Subcollection: Notes
Doc: Note 1
Collection: UserList 2
Doc: User 1
Subcollection: Notes
Doc: Note 1
您可以使用以下方法之一:
const query = ......;
query
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
console.log(doc.ref.path);
console.log(doc.ref.parent.parent.id);
});
})
在每个
QueryDocumentSnapshot
上,您都可以使用该属性,该属性返回一个DocumentReference
。然后,在此DocumentReference
上使用属性,该属性将返回完整路径,例如UserList1/User1/Notes/Doc1
或者您使用
DocumentReference
的属性,该属性返回a,然后再次使用该属性(这次是CollectionReference
的属性)来获取父级DocumentReference
,然后是该父级DocumentReference
的id
属性,非常感谢,我不知道我怎么会在文件里漏掉这个。正是我要找的!