【问题标题】:Mongoengine updating embedded documentMongoengine 更新嵌入文档
【发布时间】:2016-12-11 17:30:47
【问题描述】:

我有以下代码,我正在尝试更新列表字段中的嵌入文档。

store = store_service.get_store_from_product_id(product_id)
got_product, idx = get_product_from_store(store, product_id)

product = Product()
product.pid = got_product.pid
product.display_name = display_name
product.description = description
product.rank = rank
product.price = price
product.categories = categories
product.properties = properties

store.catalog.products[idx] = product

print store.catalog.products[idx].__unicode__()

store.save()

当我打印出我的产品时,它具有正确的值,但是当我保存它时,它并没有持续存在。没有抛出错误。有什么想法我可能做错了吗?

【问题讨论】:

    标签: python mongoengine


    【解决方案1】:

    store.catalog.products[idx] = product可以申请DictField()。对于ListField()。你可以试试:

    store.catalog.products = [product]
    

    store.catalog.products.append(product)
    

    你需要在对象上调用保存:

    store.save()
    

    atomic updates 可能会在其他情况下提供帮助:

    Store.objects(id='123400000').update_one(push__catalog__products=product)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-19
      • 2012-09-05
      • 2015-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-28
      • 1970-01-01
      相关资源
      最近更新 更多