MongoEngine-通过id从ListField提取引用 [英] MongoEngine - Pull a reference from a ListField, by id

查看:525
本文介绍了MongoEngine-通过id从ListField提取引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想仅根据引用的值从ListField(ReferenceField)中删除一些引用.

I would like to remove some references from a ListField(ReferenceField), solely based on their value.

我在以下模型中存储有关图像的信息:

I store information about images in the following Model:

class ImageUrl(Document):
    src = UrlField()
    counter = IntField()
    deleted = BooleanField()

我们将页面上遇到的图像的id存储在名为WebpageEmbeddedDocument中:

We store the ids of the images encountered on a page in an EmbeddedDocument called Webpage:

class Webpage(EmbeddedDocument):
    image_list = ListField(ReferenceField(ImageUrl))
    ...

最后,Website模型被嵌入到RawData模型中:

Finally, the Website model is embedded into a RawData model:

class RawData(Document):
    ...
    webpage = EmbeddedDocumentField(Webpage)

我想基于某些属性(例如,计数器值超过1)从RawData记录中删除对ImageUrl记录的引用,然后将这些ImageUrl记录的deleted属性设置为True.

I would like to remove references to ImageUrl records from RawData records, based some some of their attributes (eg: counter value exceeding 1), and then set the deleted attribute of these ImageUrl records to 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)

pull操作会引发以下错误: OperationError: Update failed [Cannot apply $pull/$pullAll modifier to non-array].

The pull operation raises the following error: OperationError: Update failed [Cannot apply $pull/$pullAll modifier to non-array].

据我从 http://docs.mongodb.org了解到/manual/reference/operator/pull/#_ S_pull

As i understood it from http://docs.mongodb.org/manual/reference/operator/pull/#_S_pull or How to remove a item from a list(ListField) by id in MongoEngine?, I need to specify the key of the the array from which I want to remove the value. However, in my case, I'd like to remove a value from a list... How should I do that?

非常感谢您的光临!

推荐答案

位置运算符的工作方式是,它允许您查询列表中的值,然后对该值的第一个实例执行操作,通常是更新. $pull将从列表中删除所有实例,这就是您想要的.

The way the positional operator works is it allows you to query for a value in a list and then do an action on the first instance of that value, usually an update. $pull will remove all instances from the list and this is what you want.

在具有引用的mongoengine中,您可以仅传递实例对象,例如:

In mongoengine with references you can just pass the instance object eg:

for rdata in RawData.objects(webpage__image_list=image):
    # remove image from the image_list
    rdata.update_one(pull__webpage__image_list=image)

我清理了代码,删除了重复的查询-因为您已经rdata无需重新查找该文档!

I cleaned up code, removed the duplicate queries - as you already have rdata no need to refind that document!

OperationError: Update failed [Cannot apply $pull/$pullAll modifier to non-array]这意味着您正在尝试提取需要数组的数据,并且存在一个文档,其中image_list实际上不是数组.这可能是由于在磁盘上有一个image_list实际上不是列表的文档所致.如果您尝试使用tryexcept块,则可以查看文档,该文档无法确定是否是这种情况,如果是,则需要手动迁移.

OperationError: Update failed [Cannot apply $pull/$pullAll modifier to non-array] This means that you are trying to pull data which requires an array and there is a document where image_list isn't actually an array. This is probably caused because on disk you have a document where image_list is not actually a list. If you put a try except block you can have a look at the document that fails to see if that is the case and if so it will need manually migrating.

这篇关于MongoEngine-通过id从ListField提取引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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