Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoengine updating embedded document

I have the following code and i'm trying to update an embedded document in a listfield.

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()

When I print out my product, it has the correct values, but when I save it, its not persisting. There are no errors being thrown. Any thoughts one what I could be doing wrong?

like image 925
Sector7B Avatar asked Nov 27 '25 16:11

Sector7B


1 Answers

store.catalog.products[idx] = product can be applied for DictField(). For ListField(). You can try:

store.catalog.products = [product]

or

store.catalog.products.append(product)

And you need to call save on the object:

store.save()

There is the possibility of atomic updates which can help in other cases:

Store.objects(id='123400000').update_one(push__catalog__products=product)
like image 110
Abbasov Alexander Avatar answered Dec 02 '25 06:12

Abbasov Alexander