从ListField中删除具有FileField的EmbeddedDocument [英] Deleting EmbeddedDocument with FileField from ListField

查看:94
本文介绍了从ListField中删除具有FileField的EmbeddedDocument的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MongoEngine中,当从包含FileField的ListField中删除EmbeddedDocument时,引用的文件不会被删除.目前,我已经通过遍历整个列表字段解决了该问题.

In MongoEngine, when deleting an EmbeddedDocument from a ListField which includes a FileField, the referenced file does not get deleted. Currently, I have solved the issue by looping over the whole list field.

for embdoc in doc.embdocs:
    if embdoc.filtered == value:
        embdoc.dfile.delete()
doc.update(pull__embdocs={'filtered': value})

我想知道是否有更好的方法可以做到这一点.

I was wondering if there was a better way to do this.

推荐答案

默认情况下,MongoDB不会检查数据的完整性,因此删除其他文档仍保留引用的文档将导致一致性问题.

By default, MongoDB doesn’t check the integrity of your data, so deleting documents that other documents still hold references to will lead to consistency issues.

您应将ListFieldReferenceFields一起使用. ReferenceFields可以与选项reverse_delete_rule=mongoengine.PULL或其他选项一起使用:

You should use ListField with ReferenceFields. ReferenceFields can used with option reverse_delete_rule=mongoengine.PULL or another:

mongoengine.DO_NOTHING 这是默认设置,不会执行任何操作.删除速度很快,但可能导致数据库不一致或引用悬空.

mongoengine.DO_NOTHING This is the default and won’t do anything. Deletes are fast, but may cause database inconsistency or dangling references.

mongoengine.DENY 如果仍然存在要删除的对象的引用,则删除被拒绝.

mongoengine.DENY Deletion is denied if there still exist references to the object being deleted.

mongoengine.NULLIFY 删除仍与删除对象有关的任何对象字段(使用MongoDB的"取消设置"操作),有效地取消了关系.

mongoengine.NULLIFY Any object’s fields still referring to the object being deleted are removed (using MongoDB’s "unset" operation), effectively nullifying the relationship.

mongoengine.CASCADE 首先删除所有包含引用要删除对象的字段的对象.

mongoengine.CASCADE Any object containing fields that are refererring to the object being deleted are deleted first.

mongoengine.PULL 从ListField(ReferenceField)的任何对象字段中删除对对象的引用(使用MongoDB的拉"操作).

mongoengine.PULL Removes the reference to the object (using MongoDB’s "pull" operation) from any object’s fields of ListField (ReferenceField).

这篇关于从ListField中删除具有FileField的EmbeddedDocument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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