I have a collection ( reg_schools ) that has unique document names ( Liebenberg Primary School , St Michaels Primary School ) . Then those uniquely named documents has collection ( school_news ) with random document id names that has data too , and I would like to know if I can that all that data for collection 'school_news ' for both Liebenberg Primary School and Michaels Primary School and have it in same listview . Please see my image .
At the moment I only know of getting school_news data if I enter the unique document name in Streambuilder .
Container(
height: 500,
child: StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection('reg_schools')
.doc('Libenberg Primary School')
.collection('school_news')
.orderBy('created', descending: true)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: LinearProgressIndicator(),
);
} else
return ListView(
children: snapshot.data!.docs.map((doc) {
return Padding(
So my question is , how can i get all school_news data of both Liebenberg Primary School and St Michaels Primary School in same listview ?
UPDATE
Created index as per answer given . And am able to get all the school_news collection data
Would I be able to do some filtering on what is returned with something like :
FirebaseFirestore.instance
.collectionGroup('school_news')
.where('name', arrayContainsAny: test)
.snapshots(),
I will then have a list like :
List userSelections = ['Liebenberg', 'St Michaels' , 'School C' ];
CodePudding user response:
You can use a collection group query to query documents in all subcollections of the same name, treating the entire set of documents as one unit. Use collectionGroup() for that.
FirebaseFirestore.instance
.collectionGroup('school_news')
.orderBy('created', descending: true)
.snapshots()
It is not possible to exclude any paths from that query. You must take all subcollections with the given name.