如何使用Mongoengine删除嵌入式文档?

时间:2014-03-07 02:26:32

标签: python django mongoengine

我有一个具有EmbeddedDocumentField的集合。我无法找到如何从集合中删除嵌入文档的示例。有人能为我提供一个例子或参考吗?

以下是我的设置:

  • Python 2.7.5
  • Django 1.5.5
  • MongoEngine 0.8.7

代码:

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)

1 个答案:

答案 0 :(得分:7)

您可以使用$unset MyDoc.objects.update(unset__myField=1)字段 或者使用$pull从列表中删除单个值,例如:MyDoc.objects.update(pull__myField=Value)

请参阅:http://docs.mongoengine.org/en/latest/guide/querying.html?highlight=unset#atomic-updates