The*_*ior 7 android firebase google-cloud-firestore
我正在使用一个使用以下层次结构的Firestore数据库的应用程序:parent_collection:parent_document:子集合:child_document {字符串名称}使用collectionGroup我已经能够查询子集合中具有特定名称的文档,但是我没有知道如何获取parent_document
db.collectionGroup("subcollection").whereEqualTo("name", searchText).get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()){
//here I want to get the parent id of all results
}
}
});
Run Code Online (Sandbox Code Playgroud)
做到这一点的最佳方法是什么?
该QuerySnapshot点到了一些QueryDocumentSnapshot实例。
从中QueryDocumentSnapshot,您可以获取其来源:
snapshot.getRef().getParent()
Run Code Online (Sandbox Code Playgroud)
然后父级DocumentReference是:
snapshot.getRef().getParent().getParent()
Run Code Online (Sandbox Code Playgroud)
因此,要获得父文档的子集合,请执行以下操作:
snapshot.getRef().getParent().getParent().collection("name_of_subcollection")
Run Code Online (Sandbox Code Playgroud)
是的,我同意...这本来应该更具可读性。:)
| 归档时间: |
|
| 查看次数: |
1756 次 |
| 最近记录: |