Let me say I have ‘teacher’ collection inside ‘teacher’ collection I have ‘student’ collection in Firebase, I need to display every student of every teacher in a list.
I know I can create separate student collection linking teacher id, but this is the scenario I need to work with.
Please help me how can I get every student using FirebaseFirestore query.
3
Answers
To achieve this you will query all the teachers and then all the students inside each teacher. I have implemented so I am sharing my experience with you.
First of all make two models TeacherModel and StudentModel with json serialization for your ease. I have created dummy models you can add your fields in models.
After creating models you have two options.
1- Get all data in a stream as following.
2- Get all data in future as following.
What you’re looking for is known as a collection group query, which allows you to read/query documents from all collections with a specific name.
Based on the documentation:
I was researching a solution and you have to get creative like how @Sparko recoommends. In the firestore docs they say this:
"The listCollections() method of the Cloud Firestore server client libraries lists all subcollections of a document reference.
Retrieving a list of collections is not possible with the mobile/web client libraries. You should only look up collection names as part of administrative tasks in trusted server environments. If you find that you need this capability in the mobile/web client libraries, consider restructuring your data so that subcollection names are predictable."
This is also a possible work around