pradeep
pradeep

Reputation: 790

Query parent collection using sub collection in Firestore with angular8

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

Upvotes: 1

Views: 755

Answers (1)

denniskbijo
denniskbijo

Reputation: 576

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.

Upvotes: 2

Related Questions