Home > Enterprise >  Read data from multiple sub-collections in Firestore?
Read data from multiple sub-collections in Firestore?

Time:02-11

I'm stuck on obtaining data from sub-collection from Firestore. I'm attaching my code script below for reference.

Right now I get the data from a specific sub-collection. Using this Query line:

 Query query = db.collection("Users").document("Group").collection("Group_1);

It works fine and returns the data also.

But I want to get all data from the Group document.

Below link attached to my database image.

Firestore Database Image

  Query query = db.collection("Users");

    FirestoreRecyclerOptions<PatientRegister> response = new FirestoreRecyclerOptions.Builder<PatientRegister>()
            .setQuery(query, PatientRegister.class)
            .build();

    adapter = new FirestoreRecyclerAdapter<PatientRegister, RegisterHolder>(response) {
        @Override
        public void onBindViewHolder(RegisterHolder holder, int position, PatientRegister model) {


            holder.text_1.setText(model.getPatient_name());
            holder.text_2.setText(model.getGender());
            holder.text_3.setText(model.getDatetime());
       
        @Override
        public RegisterHolder onCreateViewHolder(ViewGroup group, int i) {
            View view = LayoutInflater.from(group.getContext())
                    .inflate(R.layout.registration_list, group, false);

            return new RegisterHolder(view);
        }

        @Override
        public void one rror(FirebaseFirestoreException e) {
            Log.d("000147", e.getMessage());
        }
    };

CodePudding user response:

I had a similar problem, and the problem I think is that you can't get all data from a document, .get() method just work for collections.

In my case i just solve the problem adding a data in my documents to specify the group:

Map<String,Double> User = new HashMap<>();
User.put("name","MyUserName");
User.put("group",1);

Now to get all:

db.collection("Users").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
   @Override
   public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
       postList.clear();
       for (DocumentSnapshot dc:queryDocumentSnapshots.getDocuments()) {
          //dc get each document
    }       
}

CodePudding user response:

According to my question:

So you want to get all documents from within all collections that exist under the Group document?

And your answer from the comment:

Yes, I want to get all data inside the Group document.

Please note that there is no way you can do that. Queries in Firestore are shallow, which means that will only return documents from the collection that the query is run against. There is no way you can get documents from multiple subcollections that exist under a particular document in a single go.

To solve this, you have to perform either a separate different query for each and every sub-collection, or you can use a collection group query. However, using your actual schema you won't be able to perform such a query since your sub-collections are dynamically added. So the best option that you have is to change your schema a little bit like so:

Firestore-root
  |
  --- users (collection)
       |
       --- $uid (document)
            |
            --- groups (sub-collection)
                  |
                  --- $groupId (document)
                        |
                        --- name: "Testing"

In this way, you can simply get all groups that correspond to all users using:

FirebaseFirestore db = FirebaseFirestore.getInstance();
Query groupsQuery = db.collectionGroup("groups");
groupsQuery.get().addOnCompleteListener(/* ... */);
  • Related