对于N:M关系,在MongoDB中建议等效的级联删除是什么? [英] What is the recommended equivalent of cascaded delete in MongoDB for N:M relationships?
问题描述
假设以下模式/关系"设计建议使用级联删除之类的操作来处理删除的推荐做法是什么?
Assuming the following "schema/relationship" design what is the recommended practice for handling deletion with cascade delete like operation?
关系模式:
+---------+ +--------+
| Student |-*--------1-[Enrollment]-1--------*-| Course |
+---------+ +--------+
MongoDB:
+---------+ +--------+
| Student |-*----------------*-| Course |
+---------+ +--------+
鉴于这种经典的学生注册设计,在使用MongoDB时,在学生中收集课程,反之亦然,这似乎是一个合适的数据模型(对于关系/注册表而言,这没什么用).但是来自关系世界,我应该如何处理删除课程的语义?也就是说,当删除课程时,所有注册"记录也应删除.也就是说,我应该从每个学生记录的集合中删除该课程.看来我必须触发2个查询:一个用于删除课程,然后从每个学生的收藏集中删除它.有没有一种方法可以执行单个查询来执行这种级联删除"(如语义)而无需附加查询?数据模型需要更改吗?
Given this classic design of enrollment of students to courses, having a collection of courses in students and vice versa seems to be an appropriate data model when using MongoDB (that is nothing for the relationship/enrollment table). But coming from a relational world how should I handle the semantics of deleting a course? That is, when a course is deleted, all the "enrollment" records should be deleted too. That is, I should delete the course from the collection of each student record. It looks like I have to fire 2 queries: one for deleting the course and then to delete it from each student's collection. Is there a way to have a single query to perform this "cascade delete" like semantic without the additional query? Does the data model need to change?
注意:对于所有其他用例,上述数据模型也可以正常工作:
NOTE: For all other use cases the above data model works just fine:
- 删除学生
=>
只需删除该学生以及与其一起删除的相关课程集. - 愿意放弃课程的学生
=>
只需将其从学生课程中删除 - 添加学生/课程
=>
实质上只是将其添加到相应的表"中.
- Deleting a student
=>
just delete that student and associated collection of courses deleted along with it. - Student willing to drop a course
=>
just delete it from the student collection of courses - Add student/course
=>
just add it to corresponding 'table' in essence.
唯一棘手的事情是处理课程的删除.由于我来自关系背景并且无法弄清楚这种情况,因此我应该如何在MongoDB中处理这种情况.
The only tricky thing is handling the deletion of a course. How should I handle this scenario in MongoDB, since I hail from a relational background and am unable to figure this one out.
推荐答案
在Mongo中,您正在做的事情是最好,最优化的方法.我处在类似的情况下,在完成了N:M设计模式的所有可能实现之后,也已经达到了相同的解决方案.
What you are doing is the best and most optimal way of doing it in Mongo. I am in a similar situation and after going all possible implementations of the N:M design pattern, have also arrived to this same solution.
显然,这不是mongodb,而是更多的NoSQL概念,其中变化较小的数据(课程)可以单独保存.而且由于删除课程不会是很频繁的操作,因此它足够可行以遍历所有记录以将其删除.
Apparently, This is not a mongodb thing, but more of a concept of NoSQL, wherein, the less changing data (Courses) can be kept separately. And since deleting a Course is not going to be a very frequent operation, its feasible enough to go through all the records to remove it.
另一方面,您可以保持原样. 在您的应用程序逻辑中,只需忽略学生"文档中课程"中根本没有reference_id的课程"值即可.但是在这种情况下,必须确保不再使用旧的已删除Course_id.
On the other hand, you could let it be as it is. In your application logic, just ignore the values of Courses in the Student document that don't have a reference_id in the Course document at all. But in that case, you must make sure that old deleted Course_id's are not being reused.
或者只使用课程文档中已删除的标志并处理应用程序逻辑中的其他所有内容.
OR just use the deleted flags on the Course document and handle everything else in your application logic.
这篇关于对于N:M关系,在MongoDB中建议等效的级联删除是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!