MongoEngine / MongoEngine/mongoengine
doc.modify fails with complex Q objects
Open
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.3k
- Forks
- 1.2k
- Avg merge
- 4h 41m
- Merged PRs (30d)
- 11
Description
from mongoengine import EmbeddedDocument, Document, IntField, Q, EmbeddedDocumentField
class A(EmbeddedDocument):
v = IntField()
class B(Document):
a = EmbeddedDocumentField(A)
b = B.objects.create(a=A(v=1))
b.modify({'a__v': 1}, set__a__v=2) # modify with dict works fine
q = Q(a__v=2)
print list(B.objects(q)) # check that Q object is valid
b.modify(q, set__a__v=3) # modify with Q object fails
Last line fails like this:
...
b.modify(q, set__a__v=3)
File "/home/monitorius/rasp/.env/local/lib/python2.7/site-packages/mongoengine/document.py", line 250, in modify
updated = self._qs(**query).modify(new=True, **update)
File "/home/monitorius/rasp/.env/local/lib/python2.7/site-packages/mongoengine/queryset/base.py", line 598, in modify
query = queryset._query
File "/home/monitorius/rasp/.env/local/lib/python2.7/site-packages/mongoengine/queryset/base.py", line 1518, in _query
self._mongo_query = self._query_obj.to_query(self._document)
File "/home/monitorius/rasp/.env/local/lib/python2.7/site-packages/mongoengine/queryset/visitor.py", line 89, in to_query
query = query.accept(QueryCompilerVisitor(document))
File "/home/monitorius/rasp/.env/local/lib/python2.7/site-packages/mongoengine/queryset/visitor.py", line 155, in accept
return visitor.visit_query(self)
File "/home/monitorius/rasp/.env/local/lib/python2.7/site-packages/mongoengine/queryset/visitor.py", line 78, in visit_query
return transform.query(self.document, **query.query)
File "/home/monitorius/rasp/.env/local/lib/python2.7/site-packages/mongoengine/queryset/transform.py", line 63, in query
raise InvalidQueryError(e)
mongoengine.errors.InvalidQueryError: Cannot resolve field "a.v"
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
Reproduce the failure with the embedded-document example, then trace how Document.modify in mongoengine/document.py passes the Q object into QuerySet.modify in queryset/base.py. Read queryset/visitor.py and queryset/transform.py around field resolution, and compare this path with the working dictionary query. Done means the Q-based modify succeeds for the embedded field without regressing the existing query behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- mongodb, python
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100