MongoEngine / MongoEngine/mongoengine

Mongoengine makes conflicting modifications to embedded documents

Open
#1,509 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug
Dominant language
Python
Stars
4.3k
Forks
1.2k
Avg merge
4h 41m
Merged PRs (30d)
11

Description

Some embedded document structures trip up delta tracking and cause saves to fail. For example, take the following script:

import mongoengine

class EmbeddedDoc(mongoengine.EmbeddedDocument):
    content = mongoengine.StringField()
    docs = mongoengine.ListField(mongoengine.EmbeddedDocumentField('EmbeddedDoc'))

class Doc(mongoengine.Document):
    docs = mongoengine.ListField(mongoengine.EmbeddedDocumentField(EmbeddedDoc))

mongoengine.connect('test')
Doc.objects.delete()

d = Doc()
d.docs = [EmbeddedDoc(content='1', docs=[])]
d.save()

d = Doc.objects[0]
e = EmbeddedDoc()
e.content = '2'
d.docs[0].docs.insert(0, e)
d.save()

The final call to save() fails with an error:

Traceback (most recent call last):
  File "break-mongoengine.py", line 24, in <module>
    d.save()
  File "/home/sm/Code/ma/venv/src/mongoengine/mongoengine/document.py", line 372, in save
    raise OperationError(message % six.text_type(err))
mongoengine.errors.OperationError: Could not save document (Cannot update 'docs.0.docs.0.content' and 'docs.0.docs' at the same time)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the reproduction in break-mongoengine.py and start from the save path in mongoengine/document.py, where the OperationError is raised. Trace how the nested embedded-document changes become update operations. Done means the final d.save() succeeds without MongoDB reporting conflicting updates to docs.0.docs.0.content and docs.0.docs.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.