How do you delete an embedded document using Mongoengine?
Asked Answered
H

2

7

I have a collection that has an EmbeddedDocumentField. I'm having trouble finding examples of how to delete an embedded document from the collection. Can someone provide me with an example or reference to one?

Here is what my setup looks like:

  • Python 2.7.5
  • Django 1.5.5
  • MongoEngine 0.8.7

Code:

class Merchant(Document):
    merchant_id = StringField(max_length=50)
    merchant_name = StringField(max_length=150)
    merchant_name_search_alias = StringField(max_length=150)
    website = StringField(max_length=150)
    location = ListField(EmbeddedDocumentField(Location))
    address = StringField(max_length=50)
    city = StringField(max_length=30)
    state = StringField(max_length=20)
    zipcode = IntField()
    phone_nummber = StringField(max_length=10)
    sub_lat = FloatField()
    sub_lng = FloatField()
    country = StringField(max_length=20)
    promotion = ListField(EmbeddedDocumentField(Promotion))


class Promotion(EmbeddedDocument):
    provider_name = StringField(max_length=50)
    provider_website = URLField()
    promo_name = StringField(max_length=300)
    promo_name_search_alias = StringField(max_length=100)
    retail_price = DecimalField(precision=2, force_string=True)
    discount_price = DecimalField(precision=2, force_string=True)
    deal_url = URLField()
    buy_url = URLField()
    deal_image_url = URLField()
    description = StringField(max_length=1000)
    start_at = DateTimeField()
    end_at = DateTimeField()
    category = StringField(max_length=50)
    dq_category = StringField(max_length=50)
    keywords = StringField(max_length=100)
Hoxie answered 7/3, 2014 at 2:26 Comment(2)
Can you just update the outer document and set the embedded document to null? AFAIK an embedded document is just a field that happens to be a subtree.Suellen
I haven't tried that but seems like that would work. I'm new to MongoDB and MongoEngine so wasn't sure if there was a "proper" way to do it. Thanks!Hoxie
M
9

You can $unset a field using MyDoc.objects.update(unset__myField=1) Or using $pull to remove a single value from a list eg: MyDoc.objects.update(pull__myField=Value)

See: https://docs.mongoengine.org/guide/querying.html?highlight=unset#atomic-updates

Metalinguistics answered 7/3, 2014 at 10:8 Comment(3)
@Metalinguistics could you help me with this?Requisite
@Metalinguistics I'm facing the issue I need solution. but, the URL you have provided above is not available. Kindly do help meInflexible
is Value in this case the embedded document to be removed?Teddy
L
1
user = User_.objects(username=username).get()
for ad in user.ads:
    if str(ad["_id"]) == id:
        user.ads.remove(ad)
user.save()

this is the only way i found to remove a value from an array of embeded doc

Lindsy answered 6/2, 2022 at 20:10 Comment(1)
Be careful with this approach, if you delete a lonely document inside the list, you also delete the list. Furthermore, in the latest versions, you should use oid instead of _id. Better to use pull_adds__oid as explained by @Ross.Centrifugal

© 2022 - 2025 — McMap. All rights reserved.