Giorgos Perakis
Giorgos Perakis

Reputation: 153

Delete specific value from Embedded document using Mongoengine when in Embedded Document List Field

I have a two classes in Mongoengine that looks like that:

class User(Document):

user_id = StringField(required=True)
date_modified = DateTimeField(default=datetime.datetime.utcnow())
profile_link = URLField()
email = EmailField()
first_name = StringField()
last_name = StringField()
profile_image_metadata = EmbeddedDocumentListField(ImageMetadata)
meta = {'allow_inheritance': True,
        'index_background': True,
        'collection': 'users',
        'indexes': [{'fields': ['+user_id']}]
        }

class ImageMetadata(EmbeddedDocument):
"""
"""
img_url = URLField(unique=True)
img_content = BinaryField()
img_height = IntField(min_value=0)
img_width = IntField(min_value=0)
datetime = DateTimeField(datetime.datetime.utcnow())

So, I have the ImageMetadata class which is an Embedded document in an EmbeddedDocumentListField.

What I want to do is to get into the User's EmbeddedDocument list and delete for every ImageMetadata document the value of the img_content key.

So after the deletion, we will have all the metadata except those of the image_content

What I have tried is this:

User.objects(user_id=some_random_id).update_one(__raw__={
    '$pull': {'profile_image_metadata': {'img_content': {'$ne': None}}}})

But this deleted the whole ImageMetadata object from the list, not the img_content value.

Upvotes: 0

Views: 13

Answers (0)

Related Questions