I want to fetch learners enrolled in a specific subject.
the collectionGroup works
dbFire4 = Firebase.firestore
dbFire4.collectionGroup("ClassEnrolled")
.whereEqualTo("classID", classID)
.get()
.addOnSuccessListener {
for (doc in documents)
{
val empData = doc.toObject(LearnerModel::class.java)
learnerList.add(empData)
}
val mAdapter = LearnerAdapter(learnerList)
recViewLearnerList.adapter = mAdapter
recViewLearnerList.visibility = View.VISIBLE
}
.addOnFailureListener{e->
Log.w(TAG, "Error adding data", e)
}
But i cant seem to fetch the data from the root collection which is the learners info.
This what i want fetch.. The classID of the Learner and the information of the learner.
2
Answers
A single read operation only reads from a single collection, or a set of collection of the same name.
There is no API to read from the
ClassEnrolled
and theLearners
parent document at the same time. You will need to perform separate read operations for those.Unfortunately, this would not be possible without making multiple queries to the database. One for grabbing all of the class documents, and then more to grab each of their parents.
This answer assumes the following structure (as derived from your question’s screenshots):
Looking at this query:
The query finds all documents contained in collections named
"ClassEnrolled"
with the class ID specified in the query. To get the document that contains this class ID document, you need to get a reference to the path.Once you have the path, you can go fetch each of those documents.