【问题标题】:Query parent collection using sub collection in Firestore with angular8使用 angular8 在 Firestore 中使用子集合查询父集合
【发布时间】:2020-01-14 15:00:12
【问题描述】:

我有一个父集合(类)和该父集合的子集合(学生)。 'student' 集合有一个名为 student_id 的字段。我需要的是获取特定 student_id 的所有课程。

我花了很多时间寻找答案,但找不到答案。请帮我。

我试过这个。但不工作

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

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

请帮忙。谢谢

【问题讨论】:

标签: google-cloud-firestore angular8


【解决方案1】:

Firestore 尚不支持通过提供条件来匹配其子集合的文档来查询父集合的文档。

但是,您可以使用一种解决方法。您可以使用collections-group query在所有学生子集合中查找与studentId匹配的所有学生文档,并查询每个学生文档的父文档。

我会建议另一种数据库结构,其中StudentClass 是您用例的单独集合。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多