对于 N:M 关系,MongoDB 中级联删除的推荐等效项是什么? [英] What is the recommended equivalent of cascaded delete in MongoDB for N:M relationships?

查看:20
本文介绍了对于 N:M 关系,MongoDB 中级联删除的推荐等效项是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设下面的模式/关系"设计,用级联删除操作处理删除的推荐做法是什么?

关系架构:

<前>+---------+ +--------+|学生|-*--------1-[招生]-1--------*-|课程 |+---------+ +--------+

MongoDB:

<前>+---------+ +--------+|学生|-*----------------*-|课程 |+---------+ +--------+

鉴于这种经典的学生注册课程设计,在使用 MongoDB 时,在学生中收集课程,反之亦然似乎是一个合适的数据模型(关系/注册表没有任何意义).但是来自关系世界我应该如何处理删除课程的语义?也就是说,当一门课程被删除时,所有的注册"记录也应该被删除.也就是说,我应该从每个学生记录的集合中删除该课程.看起来我必须发出 2 个查询:一个用于删除课程,然后从每个学生的集合中删除它.有没有办法让单个查询在没有附加查询的情况下执行这种级联删除"之类的语义?数据模型需要改变吗?

注意:对于所有其他用例,上述数据模型工作正常:

  • 删除学生 => 只需删除该学生以及随之删除的相关课程集合.
  • 愿意放弃课程的学生 => 只需将其从学生课程集合中删除
  • 添加学生/课程 => 本质上只是将其添加到相应的表"中.

唯一棘手的是处理课程的删除.我应该如何在 MongoDB 中处理这种情况,因为我来自关系背景并且无法弄清楚这一点.

解决方案

你正在做的是在 Mongo 中最好和最优的方式.我遇到了类似的情况,在尝试了 N:M 设计模式的所有可能实现之后,也得出了相同的解决方案.

显然,这不是mongodb的东西,更多的是NoSQL的概念,其中,变化较少的数据(Courses)可以单独保存.而且由于删除课程不会是一个非常频繁的操作,所以它足以遍历所有记录将其删除.

另一方面,你可以保持现状.在您的应用程序逻辑中,只需忽略在 Course 文档中根本没有 reference_id 的 Student 文档中 Courses 的值.但在这种情况下,您必须确保旧的已删除 Course_id 未被重复使用.

或者只使用课程文档中已删除的标志并处理应用程序逻辑中的所有其他内容.

Assuming the following "schema/relationship" design what is the recommended practice for handling deletion with cascade delete like operation?

Relational Schema:

  +---------+                                    +--------+
  | Student |-*--------1-[Enrollment]-1--------*-| Course |
  +---------+                                    +--------+

MongoDB:

  +---------+                    +--------+
  | Student |-*----------------*-| Course |
  +---------+                    +--------+

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.

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.

解决方案

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.

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.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆