Home > Software design >  get all document from 2 collection firestore
get all document from 2 collection firestore

Time:03-28

how to retrieve data from all documents in the first collection with a clause in the second collection.
I use this code but the data doesn't show up at all.. please help me..

dbf.collection("group").document().collection("member")
.whereEqualto("iduser", "098332")
                .orderBy("updatetime", Query.Direction.DESCENDING)
            .addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
                    List<DocumentSnapshot> list = value.getDocuments();

                    datalist.clear();
                    for (DocumentSnapshot d : list) {
                        final Modelfirestore c = d.toObject(Modelfirestore.class);
                        datalist.add(c);
                    

                    }
                    mAdapterss.notifyDataSetChanged();
                }
            });

I've tried with the script above but it doesn't work

CodePudding user response:

Every time you call document() without an argument, it generates a references to a new, unique, non-existing document in your database. So you're querying a non-existing subcollection, which explains why you don't get any results.


If you want to query the member subcollection of a specific group document, specify the ID of that group document in the call to document(...).


If you want to query across all member subcollections, you can use a collection group query.


If you want to query all member collections under groups, you can use a collection group query with the trick in Sam's answer here: CollectionGroupQuery but limit search to subcollections under a particular document

  • Related