MongoEngine / mongoengine

A Python Object-Document-Mapper for working with MongoDB
http://mongoengine.org
MIT License
4.24k stars 1.23k forks source link

Problem update dictionaries with a high degree of nesting using DynamicDocument #1038

Open webus opened 9 years ago

webus commented 9 years ago

I try to repeat this:

In [1]: from mongoengine import *

In [2]: class TestData(DynamicDocument): pass

In [3]: td = TestData()

In [4]: td.mydata = [{'name':'bob','sdata':[]}]

In [5]: td.save()
Out[5]: <TestData: TestData object>

At this example we created new dynamic document. Next we want to get this document and append some data to 'sdata' key in dictionary.

In [6]: td2 = TestData.objects.first()

In [7]: td2.mydata
Out[7]: [{'name': 'bob', 'sdata': []}]

In [8]: td2.mydata[0]['sdata'].append({'office':'current'})

In [9]: td2.mydata
Out[9]: [{'name': 'bob', 'sdata': [{'office': 'current'}]}]

In [10]: td2.save()
Out[10]: <TestData: TestData object>

Let's get this document again and check our changes

In [11]: td3 = TestData.objects.first()

In [12]: td3.mydata
Out[12]: [{'name': 'bob', 'sdata': []}]

As you can see mongoengine not saved new values in 'sdata' key

I found a solution how to get around this problem

In [13]: from copy import copy

In [14]: td4 = TestData.objects.first()

In [15]: ddd = copy(td4.mydata)

In [16]: ddd
Out[16]: [{'name': 'bob', 'sdata': []}]

In [17]: ddd[0]['sdata'].append({'office':'current'})

In [18]: ddd
Out[18]: [{'name': 'bob', 'sdata': [{'office': 'current'}]}]

In [19]: td4.mydata = copy(ddd)

In [20]: td4.save()
Out[20]: <TestData: TestData object>

In [21]: td5 = TestData.objects.first()

In [22]: td5.mydata
Out[22]: [{'name': 'bob', 'sdata': [{'office': 'current'}]}]

But this is not the right way.

MRigal commented 9 years ago

Hi @webus Thanks for reporting. It's not surprising me as we had many problems with ListDict. With which version did it happened? Have you tried with the latest master, we had several fixes in the area. If still happening, feel very welcome to submit a PR

bagerard commented 6 years ago

For future reference: bug is still present in 0.16.0

bagerard commented 3 years ago

For future ref, here is a minimal reproducible snippet

class TestData(DynamicDocument):
    pass

TestData.drop_collection()

td = TestData(mydata=[{'name':'bob','sdata':[]}]).save()
print(TestData.objects.as_pymongo())

nested_list = td.mydata[0]['sdata']
nested_list.append({'office':'current'})    # Issue actually occurs here
print(td.mydata)
print(td._delta())                          # Issue is visible here
td.save()

raw_td = list(TestData.objects.as_pymongo())[0]
print(raw_td)
assert list(raw_td.keys()) == ['_id', 'mydata']                                # fails
assert raw_td["mydata"] == [{'name': 'bob', 'sdata': [{'office': 'current'}]}] # fails

Issue occurs when appending to the BaseList, MongoEngine should be marking mydata.sdata as changed, but it marks just sdata and so ends up adding sdata to the root of the object. Problem exist for listfield embedded in other dictfield/listfield at first sight