1

I have a parent collection (class) and sub collection (students) of that parent collection. 'student' collection has a field called student_id. What I need is to get all the classes for particular student_id.

I have spent much time on searching for an answer but could't find an answer. Please help me.

I have tried this one. but not working

export class ClassService {
  constructor(private db: AngularFirestore) { }

  loadClasses(studentId: String) {
    return this.db.collection('class', ref => ref.where('students.student_id', '==', studentId)).get();
  }
} 

Please help. Thanks

pradeep
  • 550
  • 4
  • 10
  • Instead of describing how you database looks like, please add a screenshot of it. – Alex Mamo Sep 13 '19 at 09:20
  • 1
    This looks a lot like [Firestore - get the parent document of a subcollection](https://stackoverflow.com/questions/56219469/firestore-get-the-parent-document-of-a-subcollection) Can you try and let me know whether it works? – sllopis Sep 13 '19 at 09:57
  • Does this answer your question? [Firestore query subcollections](https://stackoverflow.com/questions/46573014/firestore-query-subcollections) – Eduardo May 15 '20 at 17:39

1 Answers1

2

Firestore doesn't support querying of the parent collections' documents by supplying a condition to match its sub-collection's documents yet.

However there's a workaround you can use. You can use collections-group query to find all the student documents that match the studentId in all the students sub-collections and query the parent document of each student document.

I would suggest an alternative database structure where Student and Class are seperate collections for your use case.

Hope this helps.

denniskbijo
  • 576
  • 3
  • 10