MongoEngine / MongoEngine/mongoengine
Document.update() updates mongodb but not python object for lists of embedded documents
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.3k
- Forks
- 1.2k
- Avg merge
- 4h 41m
- Merged PRs (30d)
- 11
Description
I've run into a issue with updating a list of embedded documents. There is an easy way around it for now, but I think it qualifies as a bug since it doesn't do what you'd expect as a python developer. The following code will provide an explanation better than I could with just English:
from mongoengine import Document, EmbeddedDocument, StringField, \
ListField, EmbeddedDocumentField, connect
import mongomock
# create a document that contains a list of embedded documents
class Book(EmbeddedDocument):
title = StringField(required=True)
class Library(Document):
shelf = ListField(EmbeddedDocumentField(Book))
# connect to test DB
connect(db="test", host="mongomock://localhost")
# create a book and a library with that book in it
book = Book(title="My Book")
library = Library()
library["shelf"].append(book)
library.save()
# Querying the DB, we see that the book saved correctly
print(Library.objects().first()["shelf"])
# Remove the book from the library
library.update(pull__shelf=book)
# Querying the DB again, we can see that the embedded Book document was removed
print(f"Library.objects().first()['shelf'] --> {Library.objects().first()['shelf']}")
# But list of embedded documents in the python object we just updated remains unchanged
print(f"library['shelf'] --> {library['shelf']}")
Obviously it's not hard as a programmer to just query the DB again to get the updated list of embedded documents, but that isn't expected behavior from a python object perspective. Additionally, it creates an extra call to the database when the data could be retrieved locally.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the provided Python reproduction around Document.update() and the pull__shelf operation, using mongomock to observe both database and object state. Trace how the database update is applied versus the in-memory list of embedded documents. Done means the embedded Book is removed from MongoDB and the updated Python object without requiring an extra reload.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- mongodb, python
- Domain
- database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100