Firestore中的一对一关系

huangapple go评论64阅读模式
英文:

One to One relationship in Firestore

问题

我有一个学生的集合和一个教师的集合。学生文档中有教师的ID作为一个字段。
我想只保存教师的ID,以避免如果教师更新了他的信息,避免更新每一个学生,就像如果教师文档在学生内部一样。
当我收到学生的数据时,是否有一种方法可以获取教师的数据作为JSON?为了避免在教师集合中查找教师ID,我想将教师的ID作为一个字段发送,但是接收与该ID关联的教师对象。

英文:

I have a collection of students and another of teachers. Student documents have the teacher ID inside as a field.
I want to save only the teacher's ID to avoid that if the teacher updates his information avoid updating each of the students as i would have to do if you had the teacher document inside the student.
When I receive the student's data, is there a way to get the teacher's data in a json? In order to avoid making a new query to the teacher collection looking for the teacher ID, I want to send the teacher's id as a fiels but receive the Teacher object associate with that id

答案1

得分: 1

你可以在两个集合中都复制数据,或者执行两个查询。这方面没有其他选择 - 你需要选择一种策略。在Firestore中没有类似SQL的“连接”,允许查询跨多个集合。在像Firestore这样的NoSQL数据库中,通常会复制数据(称为去范式化)以提高查询速度。

参考:

英文:

You either duplicate the data in both collections, or you perform two queries. There are no alternatives to this - you need to pick a strategy. There are no SQL-like "joins" in Firestore that allow a query to span multiple collections. With NoSQL databases like Firestore, it's typical to duplicate data (called denormalization) to improve query speed.

See:

huangapple
  • 本文由 发表于 2023年7月24日 20:14:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76754404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定