简体   繁体   中英

How do you delete an embedded document using Mongoengine?

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?

  • <\/li>
  • <\/li>
  • <\/li><\/ul>

    Code:

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: http://docs.mongoengine.org/en/latest/guide/querying.html?highlight=unset#atomic-updates

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM