mongoengine reverse_delete_rule 的工作方向是什么?

In what direction does mongoengine reverse_delete_rule work?

如果我有以下两个模型:

class User(Document):
    ...

class Profile(Document):
    user = ReferenceField(reverse_delete_rule=CASCADE)

删除用户实例是否会删除其配置文件?删除其个人资料会删除用户吗?

documentation:

中似乎有错误
class Employee(Document):
    ...
    profile_page = ReferenceField('ProfilePage', reverse_delete_rule=mongoengine.NULLIFY)

The declaration in this example means that when an Employee object is removed, the ProfilePage that belongs to that employee is removed as well. If a whole batch of employees is removed, all profile pages that are linked are removed as well.

代码中使用了NULLIFY,但是解释说明使用了CASCADE。还是我误会了什么?

删除用户实例会删除其配置文件。这就是 reverse_delete_rule=CASCADE 的工作方式。就像在关系数据库中一样。

您可以查看此代码:

from mongoengine import connect, Document, ReferenceField, CASCADE


connect('test_cascade')


class User(Document):
    pass


class Profile(Document):
    user = ReferenceField(User, reverse_delete_rule=CASCADE)


user = User().save()
profile = Profile(user=user).save()

user.delete()

assert Profile.objects.count() == 0

他们还更新了文档,现在是另一种方式:

class ProfilePage(Document):
    ...
    employee = ReferenceField('Employee', reverse_delete_rule=mongoengine.CASCADE)

The declaration in this example means that when an Employee object is removed, the ProfilePage that references that employee is removed as well. If a whole batch of employees is removed, all profile pages that are linked are removed as well.