【问题标题】:How to get collection and subcollction from parent collection in cloud firestore android如何从 Cloud Firestore android 中的父集合中获取集合和子集合
【发布时间】:2021-05-18 18:16:09
【问题描述】:

我有 user_info 作为父集合。在这个父集合下,它有 single_list 作为子集合和一些信息。我想从父集合中获取所有值。请帮我找到答案。 提前致谢

【问题讨论】:

    标签: android firebase kotlin google-cloud-firestore


    【解决方案1】:

    我认为您对术语的使用有点不对劲。您有一个 user_info 文档的集合,每个文档都有一个名为 single_data 的子集合。

    这里的区别在于没有一个 single_data 子集合。每个user_info 文档都有一个子集合。

    由于子集合具有静态名称,因此您要做的事情非常简单,使用 collection group query

    firebase.firestore().collectionGroup('single_data')
        .get()
        .then((querySnapshot) => {
           // Do something with the docs from across all the subcollections
         })
    

    【讨论】:

    • 感谢格雷回答我的问题。我使用了collectionGroup,但它只给出了特定的single_list,这意味着它在user_info 之一下显示single_list。我想显示所有 user_info 的每个 single_list。
    • 查看我上面链接的文档。它描述了collectionGroup() 的概念。确保您的子集合具有一致的名称。
    • single_data 是一致的子集合,只有我知道出了什么问题。我正在获取特定的“single_list”列表
    • fireStore.collectionGroup("single_data") .orderBy("date", Query.Direction.DESCENDING) .addSnapshotListener { value, error -> if (error != null) { Log.d("错误”、“出了点问题”) return@addSnapshotListener } if (value != null){ val allUser = ArrayList() val document = value.documents
    • document.forEach{ try { val allListModel = it.toObject(AddPostInfoModel::class.java) if (allListModel != null) { allUser.add(allListModel) } }catch (e : Exception) { e.printStackTrace() } }
    【解决方案2】:
               FirebaseFirestore db = FirebaseFirestore.getInstance();
            
                                    db.collection("users_info")            
                                    .get()
                                    .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                                        @Override
                                        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                                            if (task.isSuccessful()) {
                                                for (DocumentSnapshot document : task.getResult()) {
                                                //Here you can add all documents Id to your documentIdList to fetch all sigle_data at once. 
                                                    Log.d(TAG, document.getId() + " => " + document.getData());
                    
                                                     documentIdList.add(document.getId());
                                                }
                                       getAllSubCollSingleData(documentIdList);
                                            } else {
                                                Log.w(TAG, "Error getting documents.", task.getException());
                                            }
                                        }
                                    });
                
        public void getAllSubCollSingleData(List<Int> documentIdList){
                     for(int i=0;i<documentIdList.size();i++){
                
                   
                            db.collection("users_info").document(documentIdList.get(i))(would be phone number)
                             .collection("single_data")  
                        
                                    .get()
                                    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                        @Override
                                        public void onComplete(@NonNull Task<QuerySnapshot> task) {
                                            if (task.isSuccessful()) {
                                                for (QueryDocumentSnapshot document : task.getResult()) {
                                                //Here you can get all documents in sub collection single_data
                                                
                                                }
                                            } else {
                                                Log.w(TAG, "Error getting documents.", task.getException());
                                            }
                                        }
                                    });
                }}
    

    【讨论】:

    • hi androidLearner.. 如何从“user_info”获取子集合“single_data”
    • 请查看更新后的答案@RSumanRadhakrishnan
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 2021-03-06
    • 2018-06-18
    • 1970-01-01
    • 2021-01-15
    相关资源
    最近更新 更多