skip to Main Content

This is my data structure
And nested collection

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


  1. 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 the Learners parent document at the same time. You will need to perform separate read operations for those.

    Login or Signup to reply.
  2. 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):

    // Learners/$learnerId
    {
      "learnerAffix": string,
      "learnerFirstName": string,
      "learnerLRN": string, // document ID
      "learnerLastName": string,
      /* ... */
    }
    
    // Learners/$learnerId/EnrolledClasses/$classID
    {
      "classID": string // document ID
    }
    

    Looking at this query:

    dbFire4.collectionGroup("ClassEnrolled")
        .whereEqualTo("classID", classID)
        .get()
    

    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.

    val learnerDocRef = doc
      .getReference()   // reference to the nested class document
      .getParent()      // reference to the nested EnrolledClasses collection
      .getParent()      // reference to the learner's information document
    

    Once you have the path, you can go fetch each of those documents.

    dbFire4.collectionGroup("ClassEnrolled")
        .whereEqualTo("classID", classID)
        .get()
        .onSuccessTask { qSnapshot -> 
            return Tasks.whenAllSuccess(
                qSnapshot.getDocuments()
                    .map { it.getReference().getParent().getParent().get() }
            )
        }
        .addOnSuccessListener { documents ->
            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)
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search