'How to Get Subcollection docs in Firestore Collection Group Query Flutter

I have a collection and Subcollection, I want to get the data of subcollection along with main collection data. I am using collectionGroup query, but don't know how to get subcollection's data.

Below is Screenshot and the code.

Screenshot of Firestore Data

Below is my future function of getting Querysnapshot.

Future<void> getCollectionData() async {
  await FirebaseFirestore.instance
      .collectionGroup('testsub')
      .get()
      .then((QuerySnapshot snapshot) {
    final docs = snapshot.docs;
    for (var data in docs) {
      print(data.data()); // Output: {main1: it is main1, main2: it is main2}
    }
  });
}


Solution 1:[1]

Firestore queries read from a single collection only, or from all collections with a given name. There is no way to include information from other (sub)collections within the same read operation.

So you will have to perform a separate read operation to get the information from the subcollections.

In some cases it may be possible to replicate the necessary information from the subcollections in the parent document, but that really depends on your use-case and the amount of data you expect.

Solution 2:[2]

A collection group consists of all collections with the same ID.

Therefore the collection group should be pointing to your sub-collection 'testsubc'.

So your code should be;

Future<void> getCollectionData() 
async {
  await FirebaseFirestore.instance
      .collectionGroup('testsubc')
      .get()
      .then((QuerySnapshot 
snapshot) {
    final docs = snapshot.docs;
    for (var data in docs) {
      print(data.data()); // 
Output: {main1: it is main1 sub, 
main2: it is main2 sub}
    }
  });
}

Solution 3:[3]

I just found the solution, The problem was, that I was trying to get collections of different names, which is not possible, instead, I was must set the same name of the collection and all nested subcollections. This is the way we can get all the data (docs) of nested collections.

Screenshot of Firestore

Function:

Future<void> getCollectionData() async {
  await FirebaseFirestore.instance
      .collectionGroup('testsub')
      .get()
      .then((QuerySnapshot snapshot) {
    final docs = snapshot.docs;
    for (var data in docs) {
      print(data.data());
      print(data.reference);
    }
  });
}

Output:

flutter: {main1: aaaa, main2: ssdfad}
flutter: DocumentReference<Map<String, dynamic>>(testsub/oMpahbuUZJoHP9Z6yzbD)
flutter: {main1: it is main1, main2: it is main2}
flutter: DocumentReference<Map<String, dynamic>>(testsub/xJ2k27Ss1FhvnCJWj8XG)
flutter: {id: some id, name: ABC DAA}
flutter: DocumentReference<Map<String, dynamic>>(testsub/xJ2k27Ss1FhvnCJWj8XG/testsub/Cec5ZNTnq2ScI9I9T61k)
flutter: {id: EDsj8dZLKID3euB3V011, name: some data}
flutter: DocumentReference<Map<String, dynamic>>(testsub/xJ2k27Ss1FhvnCJWj8XG/testsub/EDsj8dZLKID3euB3V011)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Frank van Puffelen
Solution 2 Lazarus Nwankwo
Solution 3 DholaSain