我想删除一些引用ListField(ReferenceField)
,仅基于其价值。
我将有关图像的信息存储在以下模型中:
class ImageUrl(Document):
src = UrlField()
counter = IntField()
deleted = BooleanField()
我们存储id
页面上遇到的图像的数量EmbeddedDocument
called Webpage
:
class Webpage(EmbeddedDocument):
image_list = ListField(ReferenceField(ImageUrl))
...
最后,Website
模型被嵌入到RawData
model:
class RawData(Document):
...
webpage = EmbeddedDocumentField(Webpage)
我想删除对以下内容的引用ImageUrl
记录来自RawData
记录,根据它们的一些属性(例如:计数器值超过1),然后设置deleted
这些的属性ImageUrl
记录到True
.
我正在做:
images = ImageUrl.objects((Q(deleted=False) & Q(counter__gt=1)).all()
for image in images:
# all RadData records containing the image in their image list
for rdata in RawData.objects(webpage__image_list__in=[image.id]:
# remove image from the image_list
RawData.objects(id=rdata.id).update_one(pull__webpage__image_list=image.id)
# set 'deleted=True' on the ImageUrl record
ImageUrl.objects(id=image.id).update_one(set__deleted=True)
The pull
操作会引发以下错误:OperationError: Update failed [Cannot apply $pull/$pullAll modifier to non-array]
.
据我了解http://docs.mongodb.org/manual/reference/operator/pull/#_S_pull http://docs.mongodb.org/manual/reference/operator/pull/#_S_pull or 如何在 MongoEngine 中通过 id 从列表(ListField)中删除项目? https://stackoverflow.com/questions/7846109/how-to-remove-a-item-from-a-listlistfield-by-id-in-mongoengine?rq=1,我需要指定要从中删除值的数组的键。但是,就我而言,我想从列表中删除一个值...我应该怎么做?
非常感谢您的宝贵时间!