如何在Firestore中检索子集合的文档?

nan*_*ndu 6 android firebase google-cloud-firestore

我有一个db结构,如下图所示: 在此输入图像描述

在这里,我需要拥有多个品牌,每个品牌下还有多个子品牌.我之前尝试使用'地图',但它确实非常混乱,并且还了解到拥有多个文档会更好.我对此非常陌生,现在已经挣扎了3-4天.任何帮助将非常感激.

  firebaseFirestore.collection("Coffee").addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
            if (e != null) {
                Log.d(TAB, "Error : " + e.getMessage());
            }
            for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {
                if (doc.getType() == DocumentChange.Type.ADDED) {
                    Log.d("Brand Name: ",doc.getDocument().getId());

                  //  how to retrieve documents of subCollection here? something like doc.getDocument().collection??

                }

            }

        }
    });
Run Code Online (Sandbox Code Playgroud)

谢谢.

Nou*_* Ch 3

这真的很简单,也很容易尝试一下。

 firebaseFirestore.collection("Coffee").addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
            if (e != null) {
                Log.d("", "Error : " + e.getMessage());
            }
            for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {
                if (doc.getType() == DocumentChange.Type.ADDED) {
                    Log.d("Brand Name: ", doc.getDocument().getId());
                    doc.getDocument().getReference().collection("SubBrands").addSnapshotListener(new EventListener<QuerySnapshot>() {
                        @Override
                        public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
                            if (e != null) {
                                Log.d("", "Error : " + e.getMessage());
                            }

                            for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {
                                if (doc.getType() == DocumentChange.Type.ADDED) {
                                    Log.d("SubBrands Name: ", doc.getDocument().getId());
                                }
                            }

                        }
                    });
                }

            }
        }});
Run Code Online (Sandbox Code Playgroud)

注意:我已经更新了答案。显然,您无法通过单个请求获取集合文档中的子集合。