英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论